What does this do, in a cpp file?
class xyz;
where xyz is defined in on of the include files.
It doesn't create an instance of xyz.
confused
What does this do, in a cpp file?
class xyz;
where xyz is defined in on of the include files.
It doesn't create an instance of xyz.
confused
This is called forward declaration: http://en.wikipedia.org/wiki/For...
An example from cplusplus.com:
//foo.h #include "bar.h" class Foo{ Bar a; }; //bar.h class Foo; class Bar{ Foo &a }; //foo.cpp #include "foo.h" //bar.cpp #include "foo.h"
It is the same concept as:
int foo(); //Declaration ... int foo() //Definition { ... }
It is the same concept as: [...]
It's the same concept on the level of "Hey, compiler! There will be something called X defined eventually...".
on the original post, I was confused about the coding of
class foo;
rather than
#include "foo.h"
Typically you would forward declare a class in a header file so that you could use it in other declarations. e.g.
class foo; int someFunc(foo *a_foo);
Then in the .cpp file you include foo.h so that you can actually dereference the pointer in the definition of someFunc().
In the post just before this post, why would you say
class foo;
instead of
#include
I'm just not getting it.
Because the .h file that you put "class foo;" into only needed to know that the class existed and not any of the specifics on how the class was defined. In the example that I gave, you are passing a pointer to a function. The declaration is only the signature which only needs to know that it takes a pointer to a class named foo. No other knowledge of how foo is implemented is needed. Only when a file actually calls the function or defines the implementation of the function does it need to know the full interface of that class. But this is nothing specific to C++. If you had a struct is C, you could do the same thing.
You need a forward declaration when class A accesses class B and class B accesses class A. Otherwise each header file includes the other and this will continue forever or until the compiler says enough already and aborts. As I recall, Microsoft's compiler quits after loading 50,000 header files.
But that situation can be taken care of by header guards. And depending on what is being done in the headers, a forward declaration alone might not be enough.
You don't even need to involve inclusion of header files in this. The following (sketchy, untested) example will be enough:
class B; // What would happen to the code that follows if you remove this forward declaration class A { private: B * pb; public: A::A() { pb = new B; } }; class B { private: A * pa; public: B::B() { pa = new A; } }; A a; B b; int main(void) { return 0; }
I don't know the answer.
I do like to see this:
A a;
I use the same name for the type and the instance but capitalize the type. If there are multiple instances I add a descriptive suffix.
Similarly I use a_p as the name of the pointer to the a instance.
Of course we use A and a for illustrative purposes. In actual code the name should be descriptive.
By the way you don't need to use the stand alone "class A".
These accomplish the same thing:
class A; A* a_p;
class A* a_p;
I don't know the answer.
class B; // What would happen to the code that follows if you remove this forward declaration
was removed then the compiler would barf when reaching
class A { private: B * pb; // <----- THIS LINE!
At that point B is not something that is known to the compiler.
"Then put the definition of B first" you say? The same problem arises since a B references (well, has a pointer to) type A.
To solve this, there must be a way of saying ''Hey compiler, you will know the details of it later, but right here and now I'll let you know up-ahead that there will be a type called "B" ''. This is what the
class B;
does.
BTW: Someone compared it to a C function prototype. After thinking about it, I don't believe that such a comparison holds all the way.
Yeah but I don't need to know. ;) I don't do that stuff. If once in a blue moon I do it, I'll figure it out then. I work on the "need to know" basis.
Well, now I am confused :
What does this do, in a cpp file?
Yeah but I don't need to know.