I have a class hierarchy in my C++ program:
class DEBase {
public:
virtual double distance(vec3) = 0;
};
class Sphere : public DEBase {
private: ...
public:
Sphere(...) {...}
...
double distance(vec3 p) {...}
};
class XZPlane : public DEBase {
...
public:
XZPlane(...) {...}
...
double distance(vec3 p) {...}
}
...
I want to pass any object deriving from DEBase
into a function.
void addObject(DEBase o) {
someVector.push_back(o);
}
However, that does not work. I get an error message saying error: cannot declare parameter 'o' to be of abstract type 'DEBase'
When I Googled a bit, I found that you can pass in objects by reference.
void addObject(DEBase &o) {
someVector.push_back(&o);
}
The function now compiles properly, however invoking it seems impossible.
I tried addObject(new Sphere(...))
, I tried addObject(&new Sphere(...))
, however, nothing works.
How can I make it work?