post  Accessing members of parent class

kunigami (14)   Link to this post
Hi all,

I have a class A and class B. Class B inherits from A. Class A has a protected member x.

Class B has a method m which calls another method m1. In m1 I access A.x and set it to some value. The wierd thing is that when I try to access A.x in m after calling m1, it seems that A.x was not set at all.

I'd like to know if there's a proper way to access class A member x from B other than A.x.

Thanks in advance,
kevinchkin (431)   Link to this post
If I understand your question correctly then the problem with what you are doing is, you are using A.x to access x. However since B is inherited from A and x is a data member of A, so when you inherit B, A's data member will be inherited to B if the data members are public or protected. So to solve the problem instead of using A.x just use x

Hope this helps!
kunigami (14)   Link to this post
Sorry, it was a silly mistake!
Actually member x was a vetor<int> type. The fact was that inside method m1, I was redefining it through:

vector<int> x(n)

and what I intended to do was:

x = vector<int>(n);

Thank you for replying.

This topic is archived - New replies not allowed.