# Automatic Predefs?

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

## 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 on other sites

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 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 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 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 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 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.

1. 1
2. 2
JoeJ
20
3. 3
4. 4
frob
12
5. 5

• 13
• 18
• 13
• 20
• 13
• ### Forum Statistics

• Total Topics
632194
• Total Posts
3004681

×