Link to home
Start Free TrialLog in
Avatar of Prasad_Balakrishnan
Prasad_Balakrishnan

asked on

can a protected member of a baseclass be called on a different object in derived class?

class D is derived from class A. Class A has a protected method virtual foo(). In the implementation of a method of class D, is it allowed to use foo() on an object of class D, but different than this?
Other words,
D::anotherFoo()
{
D otherObject;
otherObject.foo(); //Is this allowed?
}

I get a compiler warning that the method foo is protected. So I ended up declaring D as a friend to B. Please clarify the concept/scope of protectedness here.
Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

>>>> otherObject.foo(); //Is this allowed?

Yes, it compiles with VC6 and VC7.1 without problems. What warning did you get?

Regards, Alex
Avatar of Prasad_Balakrishnan
Prasad_Balakrishnan

ASKER

I think  I forgot to mention that the method is accessed from base class.
Here is teh full code, if this is stored as a file.cpp, it throws compile error that void B::b1() is protected.
//Test.cpp start
class B
{
    protected:
    void b1();
};

class D : public B
{
    public:
        void d1();
};

void B::b1()
{
}

void D::d1()
{
    D otherD;
    otherD.b1();
    B otherB;
    otherB.b1();
    b1();
}
int main()
{  
}
//Test.cpp end
instead of creating base class object and calling  method, try creating derv class object and call the method.
eg:
class base
{
protected:      void foo1()      {      }      
};
class der: public base
{
public :void foo2()
{
   der d;      
  d.foo1();
  }      
};



>> Other words,
D::anotherFoo()
{
D otherObject;
otherObject.foo(); //Is this allowed?
}

here this is possible, (assumption D is derived class and D does have the overridden method of foo())
are you sure that originally you tried creating object with class D, not C ?
 
immediate posting!!
>>here this is possible, (assumption D is derived class and D does have the overridden method of foo())
are you sure that originally you tried creating object with class D, not C ?

pls chage to :
here this is possible, (assumption D is derived class and D does'nt  have the overridden method of foo())
are you sure that originally you tried creating object with class D, not C ?
 
>>  D otherD;
    otherD.b1();

your D class doesn't have b1()' s signature???
>>>> otherB.b1();
That is not valid cause class D member functions have the right to call B::b1() for D objects but not for B objects.

'protected' has the intention to give derived classes access to the protected member functions of their *own* baseclass object. When calling otherB.b1(); there is *no* baseclass object of class D involved, hence the error.
Ok, I got it. Please let me ask one final extension of this question.
I can't copy my code here, so coming up with sample code.
I got this error when  I used base class pointer to access the protected method of base class (let's say b1()). But the object pointed to is really of derived class. This is because my findmethod only return base class pointers.
So, per your previous post, does that mean, I should dynamic_cast the base class pointer to derived class and only if succeeds, invoke b1() with the cast pointer?
I am modifying the above pasted code a bit to better explain my question:
void D::d1()
{
    D otherD;
    otherD.b1();
    B * otherB = findDObject();
    otherB->b1(); // Here otherB is REALLY an object of class D
}
ASKER CERTIFIED SOLUTION
Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial