Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

polymorphic C++ references

Tags:

I was wondering how you can do polymorphism with references, as opposed to pointers.

To clarify, see the following minimal example:

class A;  class B {   public:     A& a; ///////////////// <- #1     B();     void doStuff(); };  class A {   public:     virtual void doSmth() = 0; }; void B::doStuff() {   a.doSmth(); }  class A1 : public A {   public:     void doSmth() {     } };  B::B() : a(     *        ////////////// <- #2       (new A1)  /////////// <- #3      ) { } 

This compiles and works, but as the most important point here is that a in line #1 is a reference, so in order to be able to use it polymorphically (is that an actual word?), as shown in line #3 I have to "convert a pointer to a reference" by dereferencing it.

This strikes me as a bit odd, and I was wondering if there is a better (in the sense of cleaner) way. Is it just me?

Rationale

It would be great if I didn't need a new at all, but when declaring (!) B I have no clue how to create an instance of A1 (!) as A is a forward declaration -- A1 is implemented in the same compilation unit as B. Still, is there a real need for dynamic memory allocation in this case? How would you do this?

Sorry for the slightly twofold question.

Edit

Note: B is huge (and I cannot make a template class of it), and will go out of scope precisely when the program terminates -- a is small and makes two big modules talk to each other, it will be needed as long as the instance of B lives (there is only one).

Edit 2

I just realised, that since both A and B are effectively singletons, I can simply create a static instance of A1 in the compilation unit of B, avoiding dynamic memory allocation (even if there were two Bs they could easily use the same instance of A). To be fair, I did not post this as answer, but will accept the answer that prompted me to come up with this solution.

like image 229
bitmask Avatar asked Aug 25 '11 14:08

bitmask


People also ask

What is polymorphism reference?

A polymorphic reference is a variable that can refer to different types of objects at different points in time. It is typically compatible with the class that it refers to. For example, in the following case: Employee employee; The 'employee' is a reference variable that may refer to an instance of the Employee class.

What is a polymorphic class C++?

Polymorphism in C++ Typically, polymorphism occurs when there is a hierarchy of classes and they are related by inheritance. C++ polymorphism means that a call to a member function will cause a different function to be executed depending on the type of object that invokes the function.


2 Answers

There's nothing odd. Polymorphisms works both for pointers and references:

struct Base { }; struct Derived : Base;  void foo(Base &);  int main() {   Derived x;   foo(x);    // fine } 

You're conflating this with another issue, namely creating a reference to a dynamic object:

T * pt = new T; T & rt = *pt;  T & x = *new T;  // same effect 

Note that it's generally very bad style to track a dynamic object only by reference, because the only way to delete it is via delete &x;, and it's very hard to see that x needs cleaning up.

There are two immediate alternatives for your design: 1) make a a member object in B, or 2) make a a shared_ptr<A> or unique_ptr<A> and change the initalizer to a(new A1). It all depends on whether you actually need the polymorphic behaviour, i.e. if you have other constructors for B which assign a different derived class to a other than A1.

like image 66
Kerrek SB Avatar answered Oct 01 '22 14:10

Kerrek SB


This is indeed a bit odd. If you want a member-variable of type A1 (rather than a reference), why not just rearrange your code so that the definition of A1 appears before the definition of B?

like image 42
Oliver Charlesworth Avatar answered Oct 01 '22 13:10

Oliver Charlesworth