-3

[Edit: sorry, mixed up base & subclass initially]

Given

class Car : public Vehicle {...}

I would like to declare a global variable of type Vehicle (or &Vehicle if need be), initialized as a Car instance... something like

Vehicle &myCar = Car(red);

... except that the above doesn't compile. What matters to me is that the instance is allocated in exactly the same way as if I had used (this is for AVR programming)

 Car myCar(red);

I have searched but I just can't figure out the syntax for this??

Thanks!

franck102
  • 221
  • 4
  • 14
  • 8
    You have your concepts backwards. `Car` is a type of `Vehicle`, not the other way around. You cannot instantiate the base class then assign it to the derived class. – Cory Kramer Mar 16 '18 at 14:45
  • Use pointer type. You cannot initialize an incomplete type but only allocate – SwiftMango Mar 16 '18 at 14:46
  • What does "initialized as a Vehicle instance" mean? – eerorika Mar 16 '18 at 14:46
  • Car &myCar = Vehicle(red); is totally incorrect. What you can do is: static Car* _myCar = nullptr; static Vehicle* getCar() { if(nullptr == _myCar;) _myCar = new Car(red); return _myCar; }; And it is also needs to be synchronized to be [thread safe](https://stackoverflow.com/questions/164496/how-can-i-create-a-thread-safe-singleton-pattern-in-windows). – Victor Gubin Mar 16 '18 at 14:48
  • 6
    Looks like this is XY problem and you do not understand what you want/need to do – Slava Mar 16 '18 at 14:48
  • No you do not, problem is Vehicle is not a Car so your assignment is bogus. And why you need such thing is completely unclear and shows you do not understand how things work. – Slava Mar 16 '18 at 14:52
  • if you want this `Car &myCar = Vehicle(red);` then you are definitely on the wrong track. Please try to explain the actual problem you are trying to solve – 463035818_is_not_an_ai Mar 16 '18 at 14:52
  • @franck102 Check [this](https://stackoverflow.com/questions/15188894/why-doesnt-polymorphism-work-without-pointers-references) – Victor Gubin Mar 16 '18 at 14:53
  • what is the "virtual base class" in your title? in the question there is none.... – 463035818_is_not_an_ai Mar 16 '18 at 14:54
  • *"... except that the above doesn't compile."*. Usually the compiler issues a diagnostic message that will explain why the program is ill-formed. Also, see [mcve]. – eerorika Mar 16 '18 at 14:55
  • @all - thanks, pls see the edited version. Vehicle is the virtual base class, and the variable type I want to use later. – franck102 Mar 16 '18 at 14:55
  • @franck102 *". Vehicle is the virtual base class"* In the example code it appears to be a non-virtual base class. – eerorika Mar 16 '18 at 14:56
  • In your code `Vehicle` is not a virtual base. – melpomene Mar 16 '18 at 14:56

2 Answers2

2

You can do this:

Car myActualCar(red);
Vehicle &myCar = myActualCar;

First create a Car, then provide a Vehicle reference bound to the car.

If you don't want to expose myActualCar to the rest of the file, you can use a lambda function to hide it (this is analoguous to the IIFE technique in JavaScript):

Vehicle &myCar = []() -> Car & { static Car myActualCar; return myActualCar; }();

This technique requires C++11.

melpomene
  • 84,125
  • 8
  • 85
  • 148
  • Thanks, I was just about to post exactly that; so there is no way to avoid exposing the myActualCar variable to the rest of the program? – franck102 Mar 16 '18 at 14:59
  • There is a way in @SergeyA's (deleted) answer. I've voted to undelete it, but here it is again: `Vehicle &myCar = []() { static Car myActualCar(red); return myActualCar; }();` – melpomene Mar 16 '18 at 15:01
  • Wow :) I am accepting your answer. I don't know enough about C++ compilers and AVR C++ compilers to be 100% that the above will result in the exact same memory allocations on the target processor, so I will live with the dummy "actual" variable. Thanks! – franck102 Mar 16 '18 at 15:04
1

If you don't want to have an explicit variable of the derived type:

Vehicle &&myCar = Car(red);

The rvalue reference will extend the temporary's lifetime as needed. The advantage over melpomene's lambda solution is that that Car keeps its automatic lifetime instead of becoming static.

Quentin
  • 62,093
  • 7
  • 131
  • 191