Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Polymorphic class member variable

Tags:

c++

oop

c++11

I have a class messenger which relies on a printer instance. printer is a polymorphic base class and the actual object is passed to the messenger in the constructor.

For a non-polymorphic object, I would just do the following:

class messenger {
public:
    messenger(printer const& pp) : pp(pp) { }

    void signal(std::string const& msg) {
        pp.write(msg);
    }

private:
    printer pp;
};

But when printer is a polymorphic base class, this no longer works (slicing).

What is the best way to make this work, considering that

  1. I don’t want to pass a pointer to the constructor, and
  2. The printer class shouldn’t need a virtual clone method (= needs to rely on copy construction).

I don’t want to pass a pointer to the constructor because the rest of the API is working with real objects, not pointers and it would be confusing / inconsistent to have a pointer as an argument here.

Under C++0x, I could perhaps use a unique_ptr, together with a template constructor:

struct printer {
    virtual void write(std::string const&) const = 0;
    virtual ~printer() { } // Not actually necessary …
};

struct console_printer : public printer {
    void write(std::string const& msg) const {
        std::cout << msg << std::endl;
    }
};

class messenger {
public:
    template <typename TPrinter>
    messenger(TPrinter const& pp) : pp(new TPrinter(pp)) { }

    void signal(std::string const& msg) {
        pp->write(msg);
    }

private:
    std::unique_ptr<printer> pp;
};

int main() {
    messenger m((console_printer())); // Extra parens to prevent MVP.

    m.signal("Hello");
}

Is this the best alternative? If so, what would be the best way in pre-0x? And is there any way to get rid of the completely unnecessary copy in the constructor? Unfortunately, moving the temporary doesn’t work here (right?).

like image 450
Konrad Rudolph Avatar asked May 23 '11 12:05

Konrad Rudolph


People also ask

What is a polymorphic variable?

A polymorphic Variable is a variable that can reference more than one type of object. Polymorphic variables derive their power from interaction with inheritance, overriding and substituion. A common polymorphic variable is the implicit variable that maintains the reciever during the execution of a method.

What is a class member variable?

In object-oriented programming, a member variable (sometimes called a member field) is a variable that is associated with a specific object, and accessible for all its methods (member functions).

What is a polymorphic class?

A class that declares or inherits a virtual function is called a polymorphic class. Note that despite of the virtuality of one of its members, Polygon was a regular class, of which even an object was instantiated ( poly ), with its own definition of member area that always returns 0.

What is a polymorphic type C++?

Polymorphism in C++ means, the same entity (function or object) behaves differently in different scenarios. Consider this example: The “ +” operator in c++ can perform two specific functions at two different scenarios i.e when the “+” operator is used in numbers, it performs addition.


1 Answers

There is no way to clone polymorphic object without a virtual clone method. So you can either:

  • pass and hold a reference and ensure the printer is not destroyed before the messenger in the code constructing messenger,
  • pass and hold a smart pointer and create the printer instance with new,
  • pass a reference and create printer instance on the heap using clone method or
  • pass a reference to actual type to a template and create instance with new while you still know the type.

The last is what you suggest with C++0x std::unique_ptr, but in this case C++03 std::auto_ptr would do you exactly the same service (i.e. you don't need to move it and they are otherwise the same).

Edit: Ok, um, one more way:

  • Make the printer itself a smart pointer to the actual implementation. Than it's copyable and polymorphic at the same time at the cost of some complexity.
like image 118
Jan Hudec Avatar answered Sep 20 '22 18:09

Jan Hudec