I'm using C++. When I try to convert a derived class' pointer to base class', compiler complains "base class is ambiguous".
I did some research, and found this question How can I avoid the Diamond of Death when using multiple inheritance?. And some other similar issue.
There is a solution, is to use virtual inheritance. But I'm not able to change class definitions, because they are compiled libraries.
Is that possible to do the converting, without changing base class definitions?
A minimal sample would be:
class A {};
class B : public A {};
class C : public A {};
class D : public B, public C {};
What I want to do is:
D *d = new D;
A *a = (D *)d; // ambiguous
Is that possible to do the converting, without changing base class definitions?
Yes it is. You have to choose which direct base class to go through.
class A {};
class B : public A {};
class C : public A {};
class D : public B, public C {};
void foo(A&) {}
int main() {
D d;
foo(static_cast<B&>(d));
foo(static_cast<C&>(d));
return 0;
}
I found a way to do this converting, a little tricky though.
Following code would do the job:
A *a = (B *)d;
Related
class A { };
class B : public A {};
class C : public A, public B {};
int main()
{
C c;
A *pA = static_cast<A *>(&c);
}
In the above code, class C is derived from class A in two different paths:
1. Directly from A
2. Indirectly through B i.e. A<-B<-C
So, when I cast an object of class C into a pointer to class A, g++ on Linux reports the following error:
error: A is an ambiguous base of C
I have also tried the same code by removing the static cast as follows:
A *pA = &c;
But, I still get exactly the same error.
Is there any solution for this? BTW, the same works fine without error on Windows using Visual C++ compiler.
I know that virtual inheritance solves this but I need a solution to make this work on g++ on Linux without having to use virtual inheritance. Rather than having to use virtual inheritance, is there any way I can instead specify one of the two path and get rid of the ambiguity. Many thanks!
If you can modify C, one possible approach would be to "inject" a dummy base class between it and A:
class A { };
class B : public A {};
class AlmostA : public A {};
class C : public AlmostA, public B {};
int main()
{
C c;
A *pA = static_cast<A*>(static_cast<AlmostA*>(&c));
}
[Live example]
You can even embed the dual static casts into a function in C.
Long story short, inherit from A virtually to avoid having two distinct As being a part of C:
class A { };
class B : virtual public A {};
class C : virtual public A, public B {};
Now your code will compile and run correctly.
Unlike "regular" base classes, virtual base classes are inherited only once, regardless of the number of paths that have them. Read this Q&A for a detailed explanation of how they work.
I have the following classes :
class A { };
class B {
public:
B(){}
virtual ~B(){};
};
class B1 : public B{
public:
B1(){}
virtual ~B1(){};
};
class B2 : public B{
public:
B2(){}
virtual ~B2(){};
};
class C1 : public A, public B1{
public:
C1(){}
virtual ~C1(){};
};
class C2 : public A, public B2{
public:
C2(){}
virtual ~C2(){};
};
I want to know if an object of type B is also of type A :
B*b = new C1(); // or new B, or new C2 ...
if(dynamic_cast<A*>(b))
{
...
Is my dynamic_cast the correct way do that (it compiles and it runs)?
Thank you.
To be able to use dynamic_cast all parents of the object needs to be polymorphic. I.e. you need to add at least one virtual function to the A class (like the destructor).
Once you fix that then your dynamic_cast is fine.
Class A is different than Class B in the C++ type system. Since they are both empty, they should be same however they are instatiated in memory. Even empty classes have the identity property.
dynamic_cast<new_type> (expression) commonly is used as: dynamic_cast<derived_type> (myBase). In this case "If expression is a pointer or reference to a polymorphic type Base, and new_type is a pointer or reference to the type Derived a run-time check is performed:" see cppreference.com search for dynamic_cast. This allows you to cast the base class back down to the derived class. The programmer must know that the derived class exists.
How do I use a function in class A from class D without inheriting class A and (although I understand it's bad practise, I am not permitted to alter the inheritance at all) and not remove the inheritance from C and B in D? I cannot seem to find a way around the request for member is ambiguous error. I was (wrongly) of the understanding that no matter how far removed the
class A
{
public:
void DoEverything(int){ }
};
class B : public A
{
public:
...
};
class C : public A
{
public:
...
};
class D : public C : public B
{
public:
...
};
int main()
{
D dMan;
int i = 2;
dMan.DoEverything(i);
}
My example is loosely based on the "Ambiguous base classes (C++ only)" example found here: http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/topic/com.ibm.xlcpp8a.doc/language/ref/cplr138.htm#cplr138 but that hasn't helped me understand the problem.
First of all, to make it work just do the following changes
class B : virtual public A
...
class C : virtual public A
This problem in multiple inheritance is called the diamond problem. Check out this link to know more
http://www.cprogramming.com/tutorial/virtual_inheritance.html
I am little bit confused on referencing a sub class inside a super class in C++.
For example, given Java :
public class Entity {
protected ComplexEntity _ce;
public Entity() {
}
public ComplexEntity getCentity() {
return _ce;
}
}
Where ComplexEntity extends the entity.It works.In the sub class I call getCentity() no errors.
Now ,in C++ when I write something like that:
#pragma once
#include "maininclude.h"
#include "ExtendedEntity.h"
using namespace std;
class EntityBase
{
public:
EntityBase(void);
EntityBase(const string &name);
~EntityBase(void);
protected:
ExtendedEntity* _extc;
string _name;
};
I am getting compiler error:
error C2504: 'Entity' : base class undefined
In the classes which inherit from this Entity.Why does that happen?
Is it completely unacceptable in C++?
May be Entity must be abstract ?
I would like to get suggestions on possible workarounds.
You may consider using CRTP, cut/paste from Wikipedia:
// The Curiously Recurring Template Pattern (CRTP)
template<class Derived>
class Base
{
Derived* getDerived() { return static_cast<Derived*>(this); }
};
class Derived : public Base<Derived>
{
// ...
};
Your code is like the following:
struct D : B {}; // error: B doesn't mean anything at this point
struct B {
D *d;
};
Your header ExtendedEntity.h is trying to use the definition of Entity before Entity is defined.
You need to change your code to this:
struct D;
struct B {
D *d;
};
struct D : B {};
A class in C++ needs to know the size of all its members and of all its superclasses. Class Entity does not know the size of it's subclass ComplexEntity, unless class ComplexEntity is defined before class Entity. But then, class ComplexEntity does not know the size of its superclass Entity.
This problem exists in C++, because class members are accessed using simple offset calculation. You can work around this, by forward declaring the derived class and using pointers as members:
class Extended; // declare the derived class
class Base { // define the base class
Extended* e; // you cannot use Extended e here,
// because the class is not defined yet.
};
class Extended : public Base {}; // define the derived class
I must admit I'm having trouble formulating this question, but I'll try my best to be precise. I have tried to search for an answer to my question, but I suspect I have been unable to find what I'm looking for, as I'm not exactly sure what to call this.
I have a base class A, and several child classes inheriting from this base class. I then make another class X that inherits from some of the mentioned child classes. The problem I'm now facing is that each of the classes X inherits, have their own instance of class A. The code below should give a better understanding of what I mean.
class A;
class B : public A;
class C : public A;
class X : public B, public C;
Is there a way to make class B and C share the same instance of class A, when they are both acting as indirect base classes for the same class?
To give an example of why I want this, lets look at this code.
class A
{
int _x;
};
class B : public A
{
void outputX(){std::cout << A::_x << std::endl;
};
class C : public A
{
void setX(int x){A::_x=x;}
};
class X : public B, public C
{
C::setX(5);
// this will output an un-initialized _x,
// as B and C have their own version of A
B::outputX()
};
Now I realize this seems rather unnecessary in this example here, but in my real situation I like to think it would be a good solution if B and C shared instance of A in class X.
Is this at all possible?
You can solve this by using virtual inheritance:
class B : virtual public A;
class C : virtual public A;
class X : virtual public B, virtual public C;
See more on the diamond problem.