-1

My classes have structure:

class Base 
{
protected:
    int size;
public:
    Base(const Base &b); // copy constructor.
}
;
class Derived_1 : public Base
{
private:
    double phase;  // only Derived_1 has this private attribute.
public:
    Derived_1(int size_, double phase_)
}
class Derived_2 : public Base {};
class Derived_3 : public Base {};

I use std::pair to store the objects associated with another number.

std::vector<std::pair<int, Base>> num_base_list;
num_base_list.push_back({1, Derived_1{10, 90.0}});

How do I get the phase attribute from num_base?

I tried to use dynamic_cast

Derived_1* derived_1 = dynamic_cast<Derived_1*>(&base);
// base is one of the Base object in the num_base_list

but received this error:the operand of a runtime dynamic_cast must have a polymorphic class typeC/C++(698) .

Wendao
  • 1
  • 2
  • It's a bit unclear what you actually want to achieve and why you don't use polymorphism. Also, you can't take the address of a _type_ as in `&Base`. You need an _instance_ of `Base` if you want its address. – Ted Lyngmo May 09 '23 at 10:05
  • 2
    There are two issues in the code. (1) `std::pair` cannot store an instance of `Derived`. The initialization causes `num_Base{1, Derived_1{10, 90.0}};` object slicing. (2) dynamic casting is possible only for polymorphic classes. You make class polymorphic by making at least one function virtual. Start by making destructor virtual: `virtual ~Base() = default` which is almost always very important for the code to operate. – ALX23z May 09 '23 at 10:08
  • 4
    If you store a derived class into a base, you only get the base part copied and the rest is lost. See [What is object slicing?](https://stackoverflow.com/questions/274626/what-is-object-slicing) – BoP May 09 '23 at 10:08
  • @ALX23z I've made some changes. Can you take another look at it? – Wendao May 09 '23 at 10:13
  • 2
    It's still unclear why you don't use polymorphism and it looks like you didn't understand the comment about object slicing. Did you read the link provided by @BoP ? – Ted Lyngmo May 09 '23 at 10:16
  • 3
    Base classes shoudn't need to know anything about derived classes. This is an XY problem. – user207421 May 09 '23 at 10:21

1 Answers1

0

In order to to get it you must have a pure virtual function for the base class which the Derived_1 will implement with inheritance. In base class : virtual double getPhase() const = 0 to make the pure function. In the dervied class: override double getPhase() const and implement it in the cpp

LND
  • 1
  • 3
  • Why must there be a pure `virtual` member function? Sure, it'll postpone the problem to the other derived classes, `Derived_2` and `Derived_3`, so what's the solution for those? – Ted Lyngmo May 09 '23 at 11:10
  • beacuse you dont want to implement the method in `base` but in `derived` classes. – LND May 09 '23 at 12:57
  • _"you dont want"_ and _"must"_ are different things. Also, I may _want_ a default implementation in the base class if it makes sense. – Ted Lyngmo May 09 '23 at 13:04
  • If you want to implement it you must have something in it.since you dont have anything to implement it with it will be probably pure, thats the whole concept of pure function. For example: you cant do draw on `shape` class beacuse you dont know the shape. – LND May 10 '23 at 11:10
  • I said _"when it makes sense"_. There is no **must**. Also, in this specific case, only `Derived_1` has a _phase_ so where does that leave the other derived classes if you have a pure `virtual` in the base? You've only moved the problem without solving the bigger issue that the inheritance scheme is most probably wrong to start with. – Ted Lyngmo May 10 '23 at 11:30