Solved

C++: can't access public member of grandfather?

Posted on 2014-12-26
9
337 Views
Last Modified: 2014-12-26
So when i derive from a derived class, the child class can access the parents public members, but the grandchild class cannot access the grandparents public members?  To illustrate:

class cA
{
public:
int x;
}
class cB:cA
{
void foo() {x++;}
}
class cC:cB
{
void fum() {x++;}
}
The function foo is ok, but the function fum gives me a "member not accessible" error.

Wow, guess after all these years I never tried this.  This is surprising since my c class seems like should be a type of my a class, just like my b class.

Could someone confirm this and possibly let me know a way to achieve the intended effect?

Thanks very much for any help.
0
Comment
Question by:RonMexico
  • 5
  • 4
9 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 40518696
The problem is that you are using the default inheritance, which is 'private'., your compiler should acutlly tell you that:
cA::x' not accessible because 'cB' uses 'private' to inherit from 'cA'

Specifying 'public' solves this issue:

class cA
{
public:
int x;
};
class cB: public cA
{
void foo() {x++;}
};
class cC: public cB
{
void fum() {x++;}
};

Open in new window

0
 

Author Comment

by:RonMexico
ID: 40518705
But I have "public" before the definition of 'x', shouldny that do it??
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 40518719
No, that 'publc' specifies the acceibilty from 'outside'. The inheritance specifier defaults to 'private' if you don't provide any, see http://www.cplusplus.com/doc/tutorial/inheritance/ 

If no access level is specified for the inheritance, the compiler assumes private for classes declared with keyword class and public for those declared with struct.

Open in new window


More on that at http://www.parashift.com/c++-faq-lite/access-rules-with-priv-inherit.html ("What are the access rules with private and protected inheritance?")
0
 
LVL 86

Expert Comment

by:jkr
ID: 40518727
BTW, just to make that clear: Declaring a class member 'oublic' affects instances (objects) of that very class, inheritance access specifiers apply to a class' hierarchy. They are completely different beasts.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:RonMexico
ID: 40518732
My read of the first article (see table halfway down) is exactly what i understood: that "public" grants access to both external and derived, "protected " grants access to derived but not external, and "private" grants access to neither (only instances of that class)

Which is very different than your last post above.  Are you talking about c++?  Can you please give me a specific example of an "inheritance access specifier"?
0
 

Author Comment

by:RonMexico
ID: 40518739
Sorry, your keywords helped me to the answer... Inheritance access specifiers are given at the class definition line.  That makes sense, trying now...
0
 
LVL 86

Expert Comment

by:jkr
ID: 40518741
Well, in that article (http://www.cplusplus.com/doc/tutorial/inheritance/), scroll down a little, specifically to the table the is labeled 'Access':


This public keyword after the colon (:) denotes the most accessible level the members inherited from the class that follows it (in this case Polygon) will have from the derived class (in this case Rectangle). Since public is the most accessible level, by specifying this keyword the derived class will inherit all the members with the same levels they had in the base class.

With protected, all public members of the base class are inherited as protected in the derived class. Conversely, if the most restricting access level is specified (private), all the base class members are inherited as private and thus cannot be accessed from the derived class.
0
 

Author Comment

by:RonMexico
ID: 40518746
Thanks very much!  Seemed to fix it...
0
 
LVL 86

Expert Comment

by:jkr
ID: 40518758
You're most welcome, happy holidays! ;o)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now