Pointer to private members
class CTest
{
public:
TestFunction(/*out*/ int* p_ptr)
{
p_ptr = m_ptr;
}
private:
int* m_ptr;
}
void main()
{
int* ptr;
CTest ct;
ct.TestFunction(ptr);
cout << ptr << endl;
}
a) Is it correct to pass a pointer to a private member variable?
b) For e.g. in the TestFunction(), pointer to the member variable m_ptr is passed. Is this correct?
c) Please note that other functions are hidden for brevity.
[521 byte] By [
SuperStar] at [2007-11-18 1:40:35]

# 1 Re: Pointer to private members
Your code is wrong. i.e. the pointer won't get passed out.
You ned 2 *s eg.
fun(int** passout)
{
*passout = &privatepointer;
}
# 2 Re: Pointer to private members
Yes its correct. p_ptr will hold the address of m_ptr only in that method once you are out of that method ptr won't have address of m_ptr.
To make it accessible outside that method, you will have to change TestFunction method to:
TestFunction( int** p_ptr )
{
*p_ptr = m_ptr;
}
This is not correct as you are making private member accessible indirectly. You might as well declare it public then.
Amol
# 3 Re: Pointer to private members
Originally posted by SuperStar
class CTest
{
public:
TestFunction(/*out*/ int* p_ptr)
{
p_ptr = m_ptr;
}
private:
int* m_ptr;
}
void main()
{
int* ptr;
CTest ct;
ct.TestFunction(ptr);
cout << ptr << endl;
}
a) The function called TestFunction assigns the pointer to your member variable to a local copy of p_ptr. Therefore you haven't changed the ptr that is defined in your main() function. Run it, and you will see that ptr in main() doesn't change. If you really wanted to change the pointer in main(), the code for TestFunction would look like this:
void CTest::TestFunction( int **ptr)
{
*ptr = m_ptr;
}
int * CTest::TestFunction2()
{
return m_ptr;
}
int main()
{
int *ptr;
CTest T;
T.TestFunction( &ptr );
ptr = T.TestFunction2();
}
Either you do it using TestFunction() or you can choose to return the pointer using TestFunction2().
b) Why would you want to do this? This is an indication of bad design if you allow client code access to your members like this. Your object can be screwed up by the client code if you give them access to pointers of your member variables. At the very least pass back a const pointer, or add methods to your class that manipulate the pointer in other ways.
c) Also, why do you need a pointer as your class member? Is it because you are storing a pointer to dynamically allocated memory? If so, your class needs to supply a correct copy constructor and assignment operator to take care of the dynamic memory in case your object needs to be copied or assigned to another object. To avoid this, you can read up on the usage of vector<T> and not use pointers at all.
Regards,
Paul McKenzie
# 4 Re: Pointer to private members
In addition to the other posts on form, I'll just add that public accessor functions to private state are sometimes used to provide a controlled access to the state. These are the get and set methods one sometimes sees in classes. Some people (including myself) deprecate their usage, at least to the extreme, but they at least provide a well defined entry point for the access so that such access may be tracked or made multithread safe. In this case, however, I see no access control being applied (at least in the alternate forms supplied so far -- perhaps the OP had intended returning a second pointer to the state instead of the original, so that the dereferenced value could be altered but not the pointer itself?), so it is difficult to see any advantage to making the state public in the first place.
However, although there is no advantage, I still see such code all the time in beginning c++ tutorials and even public code...