Pointer to class???

Pages: 12
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?

1
2
3
4
5
6
7
8
namespace namespace_name {

class class_name_1 {
 public:                       
  class class_name_2 *class_pointer;
};

}


Any help would be pretty much appreciated.

Sina
Can you elaborate a little bit more?
You do know what a pointer is I suppose?
Yes I do. The thing is I did not find pointer to class in such a way.
That class is being forward declared. This:

1
2
3
class Foo {
    class Bar *ptr;
};

is the same as:

1
2
3
4
class Bar;
class Foo {
    Bar *ptr;
};

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.
Last edited on
All the following are the same thing:

1
2
3
4
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.
Last edited on
All the following are the same thing

They're not quite the same thing because of forward declarations.

I believe this is why the Windows SDK will always typedef the structs.

Rather, because the win32 api is pure C, which enforces using structs that way.
Hello filipe. Kindly explain how they would be different if class_name_2 were forward-declared. I don't see it.
If you do not forward declare class_name_2, and it's actual declaration comes after class_name_1, you may get compiler errors.
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.
You didn't say you were assuming anything, just that they were the same. This:

class Foo *ptr;

will always compile. If the class hasn't been declared yet (as in the OP), it will forward declare it and declare ptr.

Now this:

Foo *ptr;

will never compile if Foo hasn't been declared. So they are not the same thing.
Ah, I see. I thought we were all under the assumption that class_name_2 had been declared already. Apparently I was the only one. Interesting.

In the example that you show there, filipe, if class Foo hadn't been declared or forward declared, what will ptr be?
The first example forward declares Foo and declares ptr to be a pointer to Foo. The second one is not legal if Foo hasn't been declared.
So what I understood from your replies is that the statement:
 
class Foo *ptr;

is equivalent to:
 
Foo *ptr;

if the class 'Foo' is declared before.
Is that correct?
Yes.
@filipe: Understood.

@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.
Thank you guys
one last question guys: can you tell me why the following code is not working?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;
namespace Sina_NS {
class Sina {

public:
    int width, height;
	class Dim *dim;
	int area () {return (width * height);}
};
}
using namespace 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
Pages: 12