943,514 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5062
  • C++ RSS
Aug 15th, 2004
0

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

Expand Post »
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<iostream.h>

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;
Reputation Points: 10
Solved Threads: 0
Newbie Poster
the_one2003a is offline Offline
13 posts
since Aug 2004
Aug 15th, 2004
0

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

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
Reputation Points: 17
Solved Threads: 1
Junior Poster
cosi is offline Offline
153 posts
since Aug 2004
Aug 16th, 2004
0

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

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
the_one2003a is offline Offline
13 posts
since Aug 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: functions
Next Thread in C++ Forum Timeline: Why can't I use Pointers to point to a Enumurated Constant





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC