Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ Better way to assign a derived class to a base class

This seems like a silly question but I want to be able to instantiate a derived class and assign it to a base class in one line:

class A { };
class B : public A { };

// i can do this
A *base;
B derived;
base = &derived;

// i'd like to do something like this
A *base;
base = &B(); // produces a warning: taking address of temporary

I'm trying to get polymorphic behavior without using the new operator. I think the compiler is inserting a temporary reference to the return value of the constructor. Is it possible to instruct the compiler that I actually want to use that address for the assignment?

* EDIT

Here's a little context. I have something like this when parsing an xml file:

class element { virtual void load_xml(...); };
class city : public element { ... };
class state : public element { ... };

element *base;
// read some xml

if (xml_node == "city") {
  base = &city(); 
} else if (xml_node == "state") {
  base = &state();
} 

base.load_xml(...);

The idea is that each derived node knows how to hydrate itself and will override the load_xml() function to do so. I guess what I should really be doing is calling load_xml in each if block rather than try and assign each derived class to the base then call load_xml from the base at the end.

Thanks!

like image 825
JaySilk84 Avatar asked Oct 20 '11 03:10

JaySilk84


People also ask

Can you assign a derived class object to a base class object?

Object Slicing in C++ In C++, a derived class object can be assigned to a base class object, but the other way is not possible.

How do you assign a base class to a derived class?

You could write a constructor in the derived class taking a base class object as parameter, copying the values. In that case you would copy the base object and get a fully functional derived class object with default values for derived members.

Can a derived class be a base class for another class?

Derived classes acquire the properties of an existing class. The original class is called the base class. A derived class inherits member functions of base class. A derived class can be used anywhere the base class is expected.

Can you assign the address of an object of a derived class to a pointer to the base class?

similarly a derived object is a base class object (as it's a sub class), so it can be pointed to by a base class pointer. However, a base class object is not a derived class object so it can't be assigned to a derived class pointer.


1 Answers

I want to be able to instantiate a derived class and assign it to a base class in one line

An object instantiated within a single line (without using the new operator) will last only until the end of the expression that evaluates it. So by the point you have the address, the temporary does no longer exist.

I'm trying to get polymorphic behavior without using the new operator.

To do that, you need a named variable to have a valid lifetime for your object.

like image 156
K-ballo Avatar answered Sep 30 '22 00:09

K-ballo