Declare object of derived class without specifying it

Holger

There is a base-class Base and two possible derived classes DerivedA and DerivedB. How can I declare a variable without specifying (at the point of declaration) which of the two derived classes will be in use?

I have tried the following example:

#include <iostream>
#include <vector>
#include <memory>
using namespace std;
#include <stdlib.h>


struct Base
{
    int base = 0;
};

struct DerivedA : Base
{
    int x = 1;
};

struct DerivedB : Base
{
    int y = 1;
};


class Test
{
public:
    Test(int a)
    {
    Base TestObj;
    if (a==0)
    {
        DerivedA TestObj; // intention: change type of TestObj to DerivedA
    }
    else
    {
        DerivedB TestObj; // intention: change type of TestObj to DerivedB
    }

    TestObj.base = 7;

    if (a==0)
    {
        TestObj.x = 2;
    }
    else
    {
        TestObj.y = 4;
    }

    myObjs.push_back(make_shared<Base>(TestObj));
    }
private:
    vector<shared_ptr<Base>> myObjs;

};

The compiler will throw an error saying "error: ‘struct Base’ has no member named ‘x’ " (or 'y' respectively).

A trivial solution would be to include everything in the first if (a==0) / else statements using a separate myObjs.push_back call for the two cases. However, I am interested in a solution where I can stay more flexible, ideally by e.g. only changing the line 'Base TestObj;' to something more general. Thanks in advance.

NathanOliver

If you want to keep the flow the same, then you just need to make TestObj a std::shared_ptr<Base> to begin with. That would make the code

Test(int a)
{
    std::shared_ptr<Base> TestObj;
    if (a==0)
    {
        TestObj = std::make_shared<DerivedA>();
    }
    else
    {
        TestObj = std::make_shared<DerivedB>(); 
    }

    TestObj->base = 7;

    if (a==0)
    {
        static_cast<DerivedA&>(*TestObj).x = 2; // need the cast so you can set the member
    }
    else
    {
        static_cast<DerivedB&>(*TestObj).y = 4; // need the cast so you can set the member
    }

    myObjs.push_back(TestObj);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to declare an object of generic abstract class without specifying template type

Method to return derived generic class without specifying the types of the return

Declare derived class in if/else without repeating base class fields

Declare object of derived class initialising properties of base class

Forward declare class specifying size

Use Typescript Class without declare a object

Jackson databind - how to deserialize an object without specifying the target class?

How to declare an array without specifying its size, but with an initializer inside a class in C++?

Specifying class of object in clojure

How to enforce an object field or property on a derived class without type issues?

Declare an object that implements an interface without explicitly using a specific class

Reset a derived class object

Android arrayadapter without specifying object

Declare object type in class

Declare an object as abstract class

How to assign values base class properties inside a derived class object without listing them one by one?

How to use declare a function template pointer typedef without specifying template?

Derived class object - Braced init

Cast base object to derived class

Cast object to base or derived class?

Can I declare inner class as a static and access it without creating an object in surrounding class?

how can does one declare a sub-class object inside of a base class without leading to recursion errors?

Is there a way to declare members of an abstract base class that are the type of the derived class?

Instantiating TypeScript generic class without specifying type

Implementing generic interface without specifying generic class

Abstract class without defining in derived class

Convert class to derived class, without modifying it

assigning value from object without specifying the index

Is it possible to clone a polymorphic object without manually adding overridden clone method into each derived class in C++?