Will this cause an ambiguity?:
class A { ... };
class B : public A {
//...
B(const B& b);
B(const A& a);
//...
};
Update: It appears that the asker has scrubbed the class names from the question. Here is the original piece of code for which this answer is written:
class Vector2D { ... };
class Vector3D : public Vector2D {
//...
Vector3D(const Vector2D& b);
Vector3D(const Vector3D& a);
//...
};
I said "no" earlier, but I changed my mind. This is ambiguous, and I can't think of a good reason to have Vector3D
inherit from Vector2D
.
From a mathematical perspective, a 2D vector space can be extended to a 3D vector space, but there are many extensions to choose from -- they are not unique. So when you make a 3D vector class that inherits from a 2D vector class, you are making one embedding of the 2D space in the 3D space "privileged" and all other embeddings inferior. I don't think that's particularly useful.
The other thing that you are doing is you are saying that "every 3D vector is a 2D vector", which is just kind of silly. For example, maybe you write a function:
// Compute the angle between two vectors
double angle(Vector2D x, Vector2D y);
Let's suppose that you forgot to write the version for 3D vectors. Now, the compiler won't be able to give you an error message: instead, your 3D vectors will get projected into 2D space using the "privileged" projection you chose when creating the class, and you will get the wrong answer. Now suppose you add another function:
// Compute the angle between two 3D vectors
double angle(Vector3D x, Vector3D y);
Now, there's still a problem.
Vector3D x = ...;
Vector2D y = ...;
double a = angle(x, y);
This will use the 2D projection of x, and again, you won't get a compiler warning. You'll just get the wrong answer.
Summary: This is the worst kind of ambiguity: it is the kind of ambiguity where the compiler will give you the answer that you don't expect.
Vector3D should not inherit from Vector2D. It is mathematically illogical.