Question about private class member variables

Discussion in 'C++' started by RK, Feb 13, 2008.

  1. RK

    RK Guest

    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
    RK, Feb 13, 2008
    #1
    1. Advertising

  2. RK

    Guest

    On Feb 13, 8:30 am, "RK" <> 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.
    , Feb 13, 2008
    #2
    1. Advertising

  3. RK

    James Kanze Guest

    On Feb 13, 4:30 am, "RK" <> 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:
    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
    James Kanze, Feb 13, 2008
    #3
  4. RK

    RK Guest

    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
    RK, Feb 14, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,751
    Thomas Weidenfeller
    Oct 12, 2005
  2. DaveLessnau
    Replies:
    3
    Views:
    428
    Howard
    May 16, 2005
  3. Jeremy Bowers
    Replies:
    3
    Views:
    513
    Steven Bethard
    Jan 24, 2005
  4. Replies:
    9
    Views:
    942
  5. Gregor Kofler
    Replies:
    6
    Views:
    211
    Gregor Kofler
    Jun 27, 2008
Loading...

Share This Page