Hi, can any body explain to me what the function of the line 'class class_name_2 *class_pointer;' is in the following code? Could you please introduce me to a webpage in this regard?
You're telling the compiler that the name Bar refers to some class that hasn't been defined yet. Because all (non-function) pointers have the same size, the compiler can deal with a pointer to Bar (it knows its size and it knows what a pointer is). It can't dereference it, though, because it doesn't know anything about class Bar besides its name.
class class_name_2 *class_pointer;
class_name_2 *class_pointer;
class class_name_2* class_pointer;
class_name_2* class_pointer;
Does this help? The keyword class is optional. I believe this has something to do with backwards compatibility with C or at least with C coders (as classes did not exist in C). As far as I know, in C you would need to declare struct and enum variables using the keywords struct and enum:
1 2 3 4 5 6 7 8 9
//C-Style
struct MyStructType
{
int myInt;
unsigned myOtherInt;
};
//The struct keyword here is necessary
struct MyStructType myVariable;
I believe this is why the Windows SDK will always typedef the structs.
And how does that affect the pointers? Why is this going to affect the declarations? I surely can write stuff that doesn't compile, but stuff that doesn't compile shouldn't be presented here. What would be the point?
In my examples, I assume that class_name_2 has either been declared or forward-declared so that it can be compiled. My question is: Why was it implied that my pointers were not the same if the class had been forward-declared? I don't see the difference.
@Sina: Yes, they are the same thing so long the class Foo has been declared or forward-declared. It actually doesn't make a difference. The only difference arises if Foo hasn't been declared in any way at all, as explained by filipe.
#include <iostream>
usingnamespace std;
namespace Sina_NS {
class Sina {
public:
int width, height;
class Dim *dim;
int area () {return (width * height);}
};
}
usingnamespace Sina_NS;
class Dim: public Sina{
public:
int width, height;
int pp () {return (width * height);}
};
int main () {
dim->width=2;
return 0;
}
it gives the error 'dim was not declared in this scope'. I thought by this way forward declared dim as a pointer to Dim