protected by instance, not by type?

Discussion in 'C++' started by Gerd Schmitt, Sep 28, 2004.

  1. Gerd Schmitt

    Gerd Schmitt Guest

    Hi gurus et al.

    My collegues and I encouter a somewhat strange behaviour of our
    compilers and are unsure whether or not the compiler is right.
    It seems that anything declared protected in a base class can only be
    accessed by the child class if it is the same instance.
    I allway thought the C++ type system only relies on static types. Am I
    wrong?

    It would be nice if someone could enligthen me

    Gerd

    Here the sample code wich does not compile:

    class A
    {
    int privA;

    protected:
    int protA;
    void protMethA(A* a) { privA = a->privA; }

    public:
    int pub;
    void pubMethA(A* a) { privA = a->privA; }
    };


    class B : public A
    {
    int privB;

    protected:
    void protMethB(A* a)
    {
    protA = 5;
    a->protMethA(this);
    int tmp = a->protA;
    }

    public:
    void pubMethB(A* a)
    {
    protA = 3;
    a->protMethA(this);
    int tmp = a->protA;
    }
    void pubMethB2(B* rhs)
    {
    rhs->privB = 5;
    }
    };


    int main()
    {
    A a;
    B b;

    a.pubMethA(&b);
    b.pubMethB(&a);

    return 0;
    }
     
    Gerd Schmitt, Sep 28, 2004
    #1
    1. Advertising

  2. "Gerd Schmitt" <> wrote in message
    news:...
    > Hi gurus et al.
    >
    > My collegues and I encouter a somewhat strange behaviour of our
    > compilers and are unsure whether or not the compiler is right.
    > It seems that anything declared protected in a base class can only be
    > accessed by the child class if it is the same instance.


    No, that's not right. But a protected member can only be accessed by a
    pointer of the same type or derived type as the accessing class.

    class A
    {
    protected:
    int x;
    };

    class B : public A
    {
    public:
    void f(A* a, B* b)
    {
    a->x = 1; // error, A is not the same as or derived from B
    b->x = 2; // ok
    }
    };

    > I allway thought the C++ type system only relies on static types. Am I
    > wrong?


    I don't see the relevance of that. We are talknig about the access system
    not the type system. In any case the C++ type system has dynamic typing,
    e.g. virtual functions and dynamic_cast.

    john
     
    John Harrison, Sep 28, 2004
    #2
    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. Andreas Klemt
    Replies:
    2
    Views:
    562
    Andreas Klemt
    Jul 5, 2003
  2. Suzanne Vogel
    Replies:
    5
    Views:
    2,362
    Dan W.
    Dec 9, 2003
  3. Steven T. Hatton
    Replies:
    2
    Views:
    417
    tom_usenet
    Aug 16, 2004
  4. Mystifier

    Protected instance variables

    Mystifier, Jan 21, 2005, in forum: Ruby
    Replies:
    8
    Views:
    206
    trans.
    Jan 21, 2005
  5. Tricky
    Replies:
    1
    Views:
    838
    JimLewis
    Feb 10, 2012
Loading...

Share This Page