Will this cause an ambiguity?:
class A { ... };
class B : public A {
//...
B(const B& b);
B(const A& a);
//...
};
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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:
I said “no” earlier, but I changed my mind. This is ambiguous, and I can’t think of a good reason to have
Vector3Dinherit fromVector2D.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:
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:
Now, there’s still a problem.
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.