Jump to content
  • Advertisement
Sign in to follow this  
Kest

Automatic Predefs?

This topic is 2432 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

When I have a header file that defines a class which uses an object:

class MyClass
{
public:
void DoSomething(OtherClass *ptr);
private:
.....
};


The compiler complains because it doesn't know what OtherClass is. Yet, as long as I don't use specific parts of OtherClass, I can just pre-def OtherClass at the top of this header:

// at the top:
class OtherClass;


My question is: Is there a way to have the compiler be okay with this, without adding pre-defs for every variable type passed to a function in the header? Perhaps some compiler option (Microsoft Visual Studio 2010)? Or maybe some type of pointer-type-hiding mechanism?

Thank you for any help.

Share this post


Link to post
Share on other sites
Advertisement

The compiler complains because it doesn't know what OtherClass is. Yet, as long as I don't use specific parts of OtherClass, I can just pre-def OtherClass at the top of this header:

// at the top:
class OtherClass;


Yes, for what it's worth this is called a forward declaration.


My question is: Is there a way to have the compiler be okay with this, without adding pre-defs for every variable type passed to a function in the header?
[/quote]
One way might be to use precompiled headers and put your forward declarations in there, though precompiled headers have some gotchas of their own, so take care.


Or maybe some type of pointer-type-hiding mechanism?
[/quote]
You could use void*, but please don't, especially not for the sake of brevity.

Are forward declarations really that bad? Remember that in writing your code, you're talking to the compiler. The forward declaration is telling it the minimum amount of information necessary in order to be able to do its work. It's hard to say "xyz is a class" in a more succinct way than "class xyz;".

The other option is to change to a dynamic language. But if you want to stick with C++, I'd suggest just "getting over it".

Share this post


Link to post
Share on other sites

My question is: Is there a way to have the compiler be okay with this, without adding pre-defs for every variable type passed to a function in the header? Perhaps some compiler option (Microsoft Visual Studio 2010)? Or maybe some type of pointer-type-hiding mechanism?

C++ is strong type language. So we always need explicitly type declaration or forwarding.
That's very good and safe.
If some compiler can accept the use of a type without declaration or forwarding, I will feel scary.

Share this post


Link to post
Share on other sites
It would be a great IDE feature, but probably a bad idea for the compiler (it would make typos much harder to track down). Something like this might be worth investigating (caveat: I haven't tried it myself).

Share this post


Link to post
Share on other sites
C++ is strong type language. So we always need explicitly type declaration or forwarding.
That's very good and safe.
If some compiler can accept the use of a type without declaration or forwarding, I will feel scary.

The type is eventually defined, assuming it is used. A forward declaration is a programmer hint, required to make up for shortcomings in the language relating to how it is compiled and linked.

The forward declaration contains no information that isn't already somewhere on your computer. Hence why incorrect forward declarations result in errors when they don't match with what was expected.

There are performance reasons (and others) for having forward declarations, but I'd love to see a world without them.

Share this post


Link to post
Share on other sites

There are performance reasons (and others) for having forward declarations, but I'd love to see a world without them.

Performance reasons that were developed for machines 30 years ago. One pass compilation was necessary back when main memory was measured in kilobytes. Modern statically typed languages live without them quite happily.

Share this post


Link to post
Share on other sites
Performance reasons that were developed for machines 30 years ago.[/quote]

Idea behind forward declaration hasn't gone away. It definitely takes less resources to compile 100 lines of code instead of parsing several megabytes of expanded code.

It's one of those things that can be completely automated away. C++ and C to lesser extent definitely benefits from such optimization, it just cannot be performed reliably on existing code, so it's not done. Modules are constantly proposed but don't seem to make it into standard. They would eliminate one of biggest hurdles of C++ development, namely build times.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!