Question about private class member variables

R

RK

I recently compiled and ran a program where I called a global function from
within a private function (which is ok) with the address of a private class
member variable (which is not ok?).

e.g.


#include <iostream>

void myFunc(int *intptr){*intptr = 99;}

class myClass
{
public:
myClass() : myData(0){};
void myClassPublicFunc(){myClassProtectedFunc();}
void printData(){std::cout << myData << std::endl;};
private:
void myClassProtectedFunc(){myFunc(&myData);}
int myData;
};

int main()
{
myClass myClassInstance;
myClassInstance.myClassPublicFunc();
myClassInstance.printData();

return 0;
}

I was quite surprised that this actually works since it seems to defy the
member access rules of C++. How is it that I can give out to a global
function the address of a private member variable? Aren't pointers to member
variables simply an offset from the start of the object's address and not an
actual address? I looked up my copy of ISO 14882 to solve this mystery with
no luck...

If it matters, I use a compiler from a large software company in Seattle.

Thanks
 
K

kasthurirangan.balaji

I recently compiled and ran a program where I called a global function from
within a private function (which is ok) with the address of a private class
member variable (which is not ok?).

e.g.

#include <iostream>

void myFunc(int *intptr){*intptr = 99;}

class myClass
{
public:
  myClass() : myData(0){};
  void myClassPublicFunc(){myClassProtectedFunc();}
  void printData(){std::cout << myData << std::endl;};
private:
  void myClassProtectedFunc(){myFunc(&myData);}
  int myData;

};

int main()
{
  myClass myClassInstance;
  myClassInstance.myClassPublicFunc();
  myClassInstance.printData();

  return 0;

}

I was quite surprised that this actually works since it seems to defy the
member access rules of C++. How is it that I can give out to a global
function the address of a private member variable? Aren't pointers to member
variables simply an offset from the start of the object's address and not an
actual address? I looked up my copy of ISO 14882 to solve this mystery with
no luck...

If it matters, I use a compiler from a large software company in Seattle.

Thanks

It will work fine.There are no errors. Programmatically the access is
broken by the global function. The best thing is to add a comment here
stating the reason for the global and the why the private variable is
passed. Whether its class/struct, offsets hold good and its true about
the addressing. All compilers should compile this code.

Thanks,
Balaji.
 
J

James Kanze

I recently compiled and ran a program where I called a global
function from within a private function (which is ok) with the
address of a private class member variable (which is not ok?).

#include <iostream>
void myFunc(int *intptr){*intptr = 99;}
class myClass
{
public:
myClass() : myData(0){};
void myClassPublicFunc(){myClassProtectedFunc();}
void printData(){std::cout << myData << std::endl;};
private:
void myClassProtectedFunc(){myFunc(&myData);}
int myData;
};
int main()
{
myClass myClassInstance;
myClassInstance.myClassPublicFunc();
myClassInstance.printData();
return 0;
}
I was quite surprised that this actually works since it seems
to defy the member access rules of C++.

Not at all. Declaring something private in a class only limits
access to its name (and it only limits access, the name is still
visible). It's hard to see how this could be otherwise:

class Mine
{
typedef std::string S ;
public:
} ;

Other classes cannot use Mine::S, but they certainly should be
able to still use std::string.

int* globalInt ;

class Mine
{
int dontTouch ;
public:
void f( int* p ) const
{
globalInt = p ;
}
void g()
{
f( &dontTouch ) ;
}
} ;

How is the compiler to know: in f(), there's no reason to ban
the assignment, and in g(), the code is calling a member
function, who has access rights.
How is it that I can give out to a global function the address
of a private member variable? Aren't pointers to member
variables simply an offset from the start of the object's
address and not an actual address?

More or less, but I don't see what that has to do with access.
If you don't want a global function accessing your private
myData, don't pass it a pointer to myData. It's your decision,
in the class---the global function won't access your private
data unless you tell it to.
 
R

RK

Thanks for both replies. The reason I chose to call a global function is
that the function is a complex mathematical calculation with several
thousand lines. I did not want to include it in my class since I don't think
it is what I would call an intrinsic property of the class.

About the offset part - I've got it squared away after a little thinking. I
was confusing pointers to a member variable (a->*b) with the address of the
variable within an object.

Thanks.
RK
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top