Pointer to a Class ?

Hello all,

I am reading "C++ : The Complete Reference 4th Edition"

The author says ( pg 292 ) :

"No member can be an object of the class that is
being declared. (Although a member can be a pointer to the class that is being
declared.)"

Through a code I gathered He meant something like :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

class abc
{
 public:abc *p1;    // ??
 //abc a2;   not allowed
};

int main()
{
	abc a1;
	cout<<a1.p1<<endl;
        cout<<&a1;
	
	
}


But then, What is pointer to a class ? Pointer to an object of a class makes sense.
If pointer to a class means pointer to an object of the class then
shouldn't the result of "a1.p1" and "&a1" be same ?

Thanks
p1 is a member pointer of the class abc. While declaring the pointer you are specifying that the pointer CAN point to an object of class abc.

You cannot declare an object of the same class within that class because that would lead to infinite recursive definition.

a1.p1 is the value of the member pointer of the object a1.

&a1 is the address of the object a1. These two are very different things.
You mean ( in the above context )
abc *p1;

means p1 CAN point to an object of class abc , but it does not by default point to the instantiated object.
1
2
3
4
5
6
7
8
9
10
11
int main()
{
	abc a1;
	
	a1.p1=&a1;
	cout<<a1.p1<<endl;
    
	cout<<&a1;
	
	
}


This yields the same result..

Thanks a lot..
Yes you are right.
Topic archived. No new replies allowed.