access control?!?

V

vDave420

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-
 
V

Victor Bazarov

vDave420 said:
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
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top