access control?!?

Discussion in 'C++' started by vDave420, Nov 19, 2004.

  1. vDave420

    vDave420 Guest

    I feel stupid, because I can't figure out why this shouldn't work.
    I am getting this error using MS VS6 sp5 i believe.

    Is this correct?

    -----------------

    namespace Test
    {
    class Base
    {
    protected:
    virtual void DoSomething( void )=0;
    };

    class Der : public Base
    {
    public:
    void CallDoSomething( Base *base );
    protected:
    virtual void DoSomething( void );
    };
    }

    using namespace Test;

    void Der::DoSomething( void )
    {}

    void Der::CallDoSomething( Base *base )
    {
    Base *This=(Base*)this;

    base->DoSomething(); // Gives "error C2248: 'DoSomething' : cannot
    access protected member declared in class 'Test::Base'"
    this->DoSomething(); // Works fine (calls Der::DoSomething() though)
    This->DoSomething(); // Gives "error C2248: 'DoSomething' : cannot
    access protected member declared in class 'Test::Base'"
    }

    -----------------

    I don't want to have to assume that a Base* is a Der* to be able to
    call Base::DoSomething from inside of Der::CallDoSomething.

    I thought that public inheritence ( class Der : public Base ) would do
    it.

    Any ideas?

    Thanks in advance.

    -dave-
     
    vDave420, Nov 19, 2004
    #1
    1. Advertising

  2. vDave420 wrote:
    > I feel stupid, because I can't figure out why this shouldn't work.
    > I am getting this error using MS VS6 sp5 i believe.
    >
    > Is this correct?


    Yes, it actually is. The derived class object is not allowed to access
    any protected members of the base class _except_ through a pointer to
    its own type.

    >
    > -----------------
    >
    > namespace Test
    > {
    > class Base
    > {
    > protected:
    > virtual void DoSomething( void )=0;
    > };
    >
    > class Der : public Base
    > {
    > public:
    > void CallDoSomething( Base *base );
    > protected:
    > virtual void DoSomething( void );
    > };
    > }
    >
    > using namespace Test;
    >
    > void Der::DoSomething( void )
    > {}
    >
    > void Der::CallDoSomething( Base *base )
    > {
    > Base *This=(Base*)this;
    >
    > base->DoSomething(); // Gives "error C2248: 'DoSomething' : cannot
    > access protected member declared in class 'Test::Base'"


    Don't do this. If you want to call the base class function, just do

    this->Base::DoSomething();

    (of course, 'this->' is optional here)

    > this->DoSomething(); // Works fine (calls Der::DoSomething() though)
    > This->DoSomething(); // Gives "error C2248: 'DoSomething' : cannot
    > access protected member declared in class 'Test::Base'"


    Yes, rightly so.

    > }
    >
    > -----------------
    >
    > I don't want to have to assume that a Base* is a Der* to be able to
    > call Base::DoSomething from inside of Der::CallDoSomething.


    Huh?

    >
    > I thought that public inheritence ( class Der : public Base ) would do
    > it.
    >
    > Any ideas?


    See above.

    Victor
     
    Victor Bazarov, Nov 19, 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. Darren Smith
    Replies:
    5
    Views:
    5,559
    dsmith
    May 10, 2004
  2. Karl
    Replies:
    0
    Views:
    595
  3. nail
    Replies:
    0
    Views:
    1,584
  4. xz
    Replies:
    9
    Views:
    557
    James Kanze
    Jan 8, 2008
  5. Hans Östlund
    Replies:
    1
    Views:
    186
    Alessandro Zifiglio
    Feb 5, 2004
Loading...

Share This Page