Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Question about private class member variables

Reply
Thread Tools

Question about private class member variables

 
 
RK
Guest
Posts: n/a
 
      02-13-2008
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


 
Reply With Quote
 
 
 
 
kasthurirangan.balaji@gmail.com
Guest
Posts: n/a
 
      02-13-2008
On Feb 13, 8:30*am, "RK" <(E-Mail Removed)> wrote:
> 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.
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      02-13-2008
On Feb 13, 4:30 am, "RK" <(E-Mail Removed)> wrote:
> 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++.


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.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
RK
Guest
Posts: n/a
 
      02-14-2008
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Replacing a private function an keeping access to private variables Gregor Kofler Javascript 6 06-27-2008 10:24 PM
Put variables into member variables or function variables? tjumail@gmail.com C++ 9 03-23-2008 04:03 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Public Data in Private Class or Private Data in Public Class? DaveLessnau C++ 3 05-16-2005 06:53 PM
RE: Why I use private variables (WAS: RE:"private" variablesa.k.a. name mangling?) Jeremy Bowers Python 3 01-24-2005 10:52 PM



Advertisments