Is it possible to access base class public member from instance of derived class in some other locations in the program.
class base {
public:
int x;
base(int xx){
x = xx;
}
};
class derived : base {
public:
derived(int xx) : base(xx){
}
};
class main {
public:
derived * myDerived;
void m1(){
myDerived = new derived(5);
m2(myDerived);
}
void m2(derived * myDerived){
printf("%i", myDerived->x);
}
};
After above code, I got following error.
`error: 'int base::x' is inaccessible`
The problem is that you accidentally use private inheritance here
class derived : base {
This makes all base class members private in the derived class.
Change this to
class derived : public base {
and it will work as expected.
You inherit privately from the base class. What you typically need is public inheritance:
class derived : public base
Here is the FAQ on private inheritance.
You should inherit from base publicly, then.
class derived : public base {
public:
derived(int xx) : base(xx){
}
};
Private inheritance is used in very specific circumstances, such as when you have a has-a relationship between two classes, but you also need to override a member of the base class.
From outside the class, you can only access public members of public base classes; and inheritance is private by default when you define a class using the class keyword.
To make it accessible, you need public inheritance:
class derived : public base
^^^^^^
Try:
class derived : public base {
...
};
Use public inheritance:
class derived : public base {
...
};
or
Make x private instead of public and use following code:
class Base {
int x;
public:
Base (int xx) {
x = xx;
}
void display() {
cout << "x = " << x << endl;
}
};
class Derived : public Base {
public:
Derived (int xx) : Base (xx) {
}
};
int main() {
Derived d1(2);
Derived *d = new Derived(10);
d->display();
d1.display();
return 0;
}
Related
I have diamond problem when inheriting from templated base class which takes Derived class as template argument.
Normaly this problem is solvable with virtual inheritance like this:
class Base0
{
protected:
int var = 0;
};
class Base1 : public virtual Base0
{
};
class Base2 : public virtual Base0
{
};
class Derived :
public Base1,
public Base2
{
public:
void f()
{
var = 1; // OK single var
}
};
However I have this scenario:
template<typename DERIVED_CLASS>
class TemplatedBase0
{
protected:
int var = 0;
};
class Base1 : public virtual TemplatedBase0<Base1>
{
};
class Base2 : public virtual TemplatedBase0<Base2>
{
};
class Derived :
public Base1,
public Base2
{
public:
void f()
{
var = 1; // ERROR: var is ambigous
}
};
I understand that in above case templated base class is not the same, ie. there are 2 completely unrealted base classes involved and it looks like virtual inheritance dosnt work.
So my question is, is there any way to make this work? what design/approach should I take here?
I need to have base class as a template which takes derived class type. but how to make diamond inheritance possible?
If want to only have a single int var; in Derived, you need to move it to a non-template base class:
class GenericTemplatedBase0
{
protected:
int var = 0;
};
template<typename DERIVED_CLASS>
class TemplatedBase0 : virtual public GenericTemplatedBase0
{
// ...
};
class Base1 : public TemplatedBase0<Base1>
{
};
class Base2 : public TemplatedBase0<Base2>
{
};
class Derived : public Base1, public Base2
{
public:
void f()
{
var = 1;
}
};
There is no diamond in your code. Your Derived has two different vars. Depending on which you want you write:
Base1::var = 1;
Base2::var = 1;
but how to make diamond inheritance possible?
It is not clear why you want a diamond. If your aim is to have only a single var in Derived you need a different design.
Consider a base class that has an attribute
class Base
{
protected:
AttributeBase * elementPtr;
...
};
And a derived class
class Derived : public Base
{
...
};
Also I have a class AttributeDerived which derives from AttributeBase
When I create an object of the class Base I would like elementPtr to be initialized in this way:
elementPtr = new AttributeBase()
But when I create an object of the class Derived I would like elementPtr to be initialized in this way:
elementPtr = new AttributeDerived()
What is the cleanest way to do that?
You could add a protected constructor to Base which allows the derived class to pass an elementPtr to use:
Base (AttributeBase* elementPtr) : elementPtr(elementPtr)
{}
Then in your derived class, call that constructor:
Derived() : Base(new AttributeDerived())
{}
If you use C++11, you could then have other Base constructors delegate to the protected one to limit code duplication.
You could add different ctors of Base for different purpose:
class Base
{
protected:
AttributeBase * elementPtr;
Base(AttributeBase * p) : elementPtr(p) {}
public:
Base() : elementPtr(new AttributeBase) {}
};
class Derived : public Base
{
public:
Derived() : Base(new AttributeDerived) {}
};
And then
Base b; // elementPtr = new AttributeBase()
Derived d; // elementPtr = new AttributeDerived()
Consider this example of code:
class Base
{
public:
Base() {}
};
class Derived1 : public Base
{
public:
Derived1() : Base() {}
};
class Derived2 : public Base
{
public:
Derived2() : Base() {}
};
Is there any to make that Derived1 has-a Derived2 and Derived2 has-a Derived1?
The best solution would be by using a third class which has those two objects. But in case high performance is needed?
An example might be a two-way client-server application.
EDIT: Consider that that's just a summary example. In the real code each of the three classes could throw an exception; I made sure that the code is exception-safe, though.
You can accomplish a "has-a" relationship with a forward declaration which basically tells "this class exists, it's just not declared yet"
#include <iostream>
using namespace std;
class Base
{
public:
Base() {}
};
// Forward declaration
class Derived1;
class Derived2 : public Base
{
friend class Derived1;
public:
Derived2() : Base() {}
private:
Derived1 *ptr;
};
class Derived1 : public Base
{
public:
Derived1(Derived2& obj) : Base(), ptr(&obj) {
obj.ptr = this;
}
private:
Derived2 *ptr;
};
int main() {
Derived2 obj2;
Derived1 obj1(obj2);
return 0;
}
http://ideone.com/RVU8AR
This way the two classes can communicate with each other. Notice the private pointers and the initialization into the constructor. With the "friend" declaration one class is able to modify the other class's private members.
Each class can hold a pointer:?
class Derived1
Derived2 *p_d2;
class Derived2
Derived1 *p_d1;
let's say we have class A,B,C,D where A is base, B,C are between and D is derived in diamond model.
NOTE:
class B inherits virtualy class A in private mode,
class C inherita virtualy class A in protected mode.
class A
{
public:
int member; // note this member
};
class B :
virtual private A // note private
{
};
class C :
virtual protected A // note protected
{
};
class D :
public B, // doesn't metter public or whatever here
public C
{
};
int main()
{
D test;
test.member = 0; // WHAT IS member? protected or private member?
cin.ignore();
return 0;
}
now when we make an instance of class D what will member be then? private or protected lol?
Figure No2:
what if we make it so:
class B :
virtual public A // note public this time!
{
};
class C :
virtual protected A // same as before
{
};
I suppose member will be public in this second example isn it?
§11.6 Multiple access [class.paths]
If a name can be reached by several paths through a multiple inheritance graph, the access is that of the path that gives most access. [ Example:
class W { public: void f(); };
class A : private virtual W { };
class B : public virtual W { };
class C : public A, public B {
void f() { W::f(); } // OK
};
Since W::f() is available to C::f() along the public path through B, access is allowed. —end example ]
I think I don't need to add anything else, but see also this defect report (which was closed as "not a defect").
when does ambiguity arise in multiple inheritance?
When you have replicated base class in several paths of inheritance and you are trying to cast to it or call its member-function.
struct A { };
struct B : A { };
struct C : A { };
struct D : B, C { }; // has replicated A as the base class
D d;
A* a = static_cast<A*>(&d); // oops
The problem has several remedies which depend on the context heavily (using virtual base classes, just refine the aforementioned cast, etc.)
More info here, especially here.
One famous example of ambiguity in multiple inheritance is the so-called Diamond Problem.
Summary:
"In object-oriented programming languages with multiple inheritance and knowledge organization, the diamond problem is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C. If a method in D calls a method defined in A (and does not override the method), and B and C have overridden that method differently, then from which class does it inherit: B, or C?"
You can find details here: Wikipedia: Diamond Problem
struct Base{
void foo(){
}
};
struct Derived1 : public Base{
};
struct Derived2 : public Base{
};
struct Final : public Derived1, public Derived2{
};
int main(){
Final f;
f.foo();
}
See on Ideone. To fix, simply use virtual inheritance:
struct Derived1 : virtual public Base{
};
struct Derived2 : virtual public Base{
};
Another possibility for ambigouty is the following:
struct Base1{
void foo(){
}
};
struct Base2{
void foo(){
}
};
struct Final : public Base1, public Base2{
};
int main(){
Final f;
f.foo();
}
Again, on Ideone. To fix, simple make do the following in Final:
struct Final : public Base1, public Base2{
using Base1::foo;
// or
// using Base2::foo;
};
When it makes names used unclear
class baseX
{
private:
void* callA();//will never be ambiguous.
protected:
void* callB();
public:
void* callC();
}
class baseY
{
private:
void* callA();//will never be ambiguous.
protected:
void* callB();
public:
void* callC();
}
class derived: public baseX, public baseY
{
void someMethod()
{
void* x = baseX::callB();//not ambiguous
void* y = baseY::callB();//not ambiguous
void* z = callB();//ambiguose
}
}
void someFunction(derived& d)
{
void* x = d.CallC();//ambiguous
}
Ambiguity can also happen when the same class is the base through more than one route:
class Base
{
public void call();
}
class DerivedX : public Base
{
}
class DerivedY : public Base
{
}
class GrandChild : public DerivedX, public DerivedY //What does call() do?
{
}
This can be solved with virtual bases:
class Base
{
public void call();
}
class DerivedX : public virtual Base
{
}
class DerivedY : public virtual Base
{
}
class GrandChild : public DerivedX, public DerivedY //only one "Base" class in inheritance, shared between DerivedX and DerivedY
{
}