0

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?

Mateon1
  • 358
  • 2
  • 14

3 Answers3

2

(I already answered this in chat, two hours ago. Here's what I said.)

It completely depends. This is not a question about polymorphism; it is a question about object ownership and about how you intend to construct your objects.

Right now your biggest problem is that you're trying to pass a pointer (or, in your nonsensical latter example, a pointer to a pointer!) when the function does not expect one. Remember, references are not pointers.

It does kind of seem like accepting pointers into that function would make a bit more sense. Consider making it some kind of C++11 smart pointer.

But, again, this has absolutely nothing to do with inheritance or polymorphism (besides the fact that using it prevents you from taking the new objects by value).

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
1

You are mixing references and pointers; What are the differences between a pointer variable and a reference variable in C++? covers the differences.

If you want to stick with references, you could use:

Sphere globe(...);

addObject(globe);

If you are creating Sphere's on the fly, using new, you could use:

Sphere *globe = new Sphere(...);

addObject(*globe);
Community
  • 1
  • 1
Phil Lello
  • 8,377
  • 2
  • 25
  • 34
  • 1
    But why would you use `new` in this case? – juanchopanza Jan 25 '15 at 19:26
  • @juanchopanza I'm building a scene with many objects for my raymarcher. It would be excessive to write multiple lines of code for each object created. – Mateon1 Jan 25 '15 at 19:29
  • @Mateon1 That has nothing to do with anything I said. – juanchopanza Jan 25 '15 at 19:30
  • @juanchopanza It depends on what the requirements are. Using new in the calling function creates an instance of the class that will still exist after the calling function exits; using a function-scoped instance will result in the object memory being reclaimed when the calling function exits. Perhaps you should follow your own recommendation to look up what new does? – Phil Lello Jan 25 '15 at 19:33
0

x *ptr=new x; candidateFunction(*ptr);

new returns a pointer, not a reference to a value

basav
  • 1,475
  • 12
  • 20