954,206 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to access a Public Pointer value of a class directly?

Hi
I made a class that has a public integer pointer
and I use this pointer in main like code below: (Just a sample code,I know about memory leaks and other related bugs,just a sample)

#include

class Cat
{
public:
int *Pointer;
};

main()
{
Cat *pCat;
pCat=new Cat;

pCat->Pointer = new int;
pCat->*Pointer=34 //this makes a compiler error

delete pCat->Pointer;
return 0;

}
I need to access this(Pointer) public pointer of class cat directly not from class methods;

the_one2003a
Newbie Poster
13 posts since Aug 2004
Reputation Points: 10
Solved Threads: 0
 

pCat->Pointer says give me the member variable of the pCat instance object. pCat->Pointer is type int*. So we can do this *(pCat->Pointer) to get at the value.

Because of operator precedence rules, this also works *pCat->Pointer.

Hope this helps!


Ed

cosi
Junior Poster
153 posts since Aug 2004
Reputation Points: 17
Solved Threads: 1
 

Hi Cosi and thanks for your great help
it solved my prob.
I hope to be able to help you in this stuff

sincerely
the_one2003a

the_one2003a
Newbie Poster
13 posts since Aug 2004
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You