开发者

C++: Accessing Grandparent Method

Why doesn't this work, and what would be a good alternative?

class Grandparent
{
    void DoSomething( int number );
};

class Parent : Grandparent
{
};

clas开发者_Go百科s Child : Parent
{
    void DoSomething()
    {
        Grandparent::DoSomething( 10 ); // Does not work.
        Parent::DoSomething( 10 ); // Does not work.
    }
};


class Grandparent
{
protected:
    void DoSomething( int number );
};

class Parent : protected Grandparent
{
};

class Child : Parent
{
    void DoSomething()
    {
        Grandparent::DoSomething( 10 ); //Now it works
        Parent::DoSomething( 10 ); // Now it works.
    }
};

At a minimum it needs to look like that. Things are private by default when working with classes, this includes from subclasses.

http://codepad.org/xRhc5ig4

There is a full example that compiles and runs.


I guess you code didn't compile - and principal source of error was missing return type specifier for method Child::DoSomething().


class Grandparent
{
    public:
    void DoSomething( int number ){
        cout<<number;
    }
};

class Parent : public Grandparent
{
    public:
    void DoSomething(int x){
        cout<<x<<'\n';
    }
};

class Child : public Parent
{
    public:
    void DoSomething()
    {
        Grandparent::DoSomething( 10 ); // works.
        Parent::DoSomething( 10 ); // works.
    }
};

Default inheritance type in c++ is private and also default member function access specifier in C++ is Private. So, you can't access private member of GrandParent class under any circumstances. You have make DoSomething() of GrandParent as either public or protected. And then mention appropriate inheritance type .


(1) Yes, we can only access grandparents members in derived class when we inherit it either using public or protected access modifier.

(2) Base Class members declared as protected are inaccessible outside the class , but they can be accessed by any derived class of that class . When you inherit protected members of base class via public or protected access modifier , that data members access specifier is protected in the derived class .

(3) By default access modifier is private in C++ in inheritance which is in your given example . Since, you are trying to access private members of base class in child class , it is bound to give compile time error . Please read next point also.

(4) Base Class members declared as private are of course inherited to derived class . But , they are not accessible irrespective of the access modifier used while inheriting . It is the matter of availability v/s accessibility . Private members of base class are available to subsequent derived class but are not accessible . We can check this using sizeof(derived class object) operator after privately inheriting base class .

Lets discuss another thing .How do we access private members of base class ? Well , we can do that by using friend functions and pointers . But , Declaring Friend function inside base class depends on the creator of that base class . So , we wont talk about that in this post .

class Base
{
  private : 
           int x1,x2;
};

class Derived : public Base
{
     public : 
              int x3;
              void display(int *ptr)
              {
                   cout << "the value of x1 = " << *(ptr) << endl;
                   cout << "the value of x2 = " << *(ptr+1) << endl;
                   cout << "the value of x3 = " << *(ptr+2) << endl;
              }
};

int main()
{
    Derived d;
    int *ptr = (int *)&d.x3; // typecasting 

    *ptr = 3; // setting x3 as 3
     ptr--;
     
    *ptr = 2; // setting x2 as 2
     ptr--;
 
    *ptr = 1; // setting x1 as 1
     ptr--;

     d.display(ptr);
     return 0;
}

We have succesfully accessed private members of base class .

One more thing is there that i want to share is that : if we have a virtual grand parent class , we can directly call the grand parent constructor from child class . But in general it is not allowed to call grandparents constructor directly , it has to be called through parent classs . It is allowed only when virtual keyword is used .


Its Multi-Level Inheritance try mentioning the access specifier for your grandparent class.

Cause by default the access specifier is PRIVATE and we wont be able to access the private members of a parent class in the child class.


You have to add access specifier At second,sixth and tenth line.

class Grandparent
    { public:
    void DoSomething( int number);
     };

class Parent : public Grandparent{
  };

 class Child :public Parent{

    void DoSomething(){
        Grandparent::DoSomething( 10 );
        Parent::DoSomething( 10 );

    }
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜