casting a vector of pointers

Apr 20, 2020 at 6:14pm
first time posting here :) be nice please.

let's say I want to do something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  #include <vector>
#include <iostream>

struct Parent{};
struct Son : public Parent{
    void say_hello(){std::cout<<"hello";};
};
struct Son2 : public Parent{
    void say_bye(){std::cout<<"bye";};
};

int main(){
    std::vector<Parent*> son_vector;
    std::vector<Parent*> son2_vector;

    son_vector.emplace_back(new Son);
    son2_vector.emplace_back(new Son2);

    son_vector.at(0)->say_hello(); //error
    son2_vector.at(0)->say_bye();  //error
};


I know why it fails, but I don't know how to solve it.
first of all, should I be trying to do something like this? I have though of creating all the virtual functions needed in the parent class, but for the real code it would be a mess, since there are a lot of sub-classes and are quite different between them.

another way to solve it that I have tried is to cast a std::vector<Parent*> into std::vector<Son*> but the compiler says no in almost everything I try, is it possible to do it? and again should I be trying to do it?

any other thing that I could try to achieve my goal in a more elegant way?
Last edited on Apr 20, 2020 at 6:18pm
Apr 20, 2020 at 6:24pm
There needs to be some sort of coherent design. It's hard for me to speak generally about this, because it really depends on the design.

Start from the ground up. What actual actions should a Parent object actually be able to do?
What is the end goal of having these Son and Son2 classes be derived from parent? What common functionality are you trying connect by having them all in a list?

Generally, yes, if you want some common, but dynamic functionality, you should have the base class have a virtual function, and then what that function actually does can be customized in the subclasses.

If they are doing completely different actions (say_hello, say_bye), perhaps you should store them separately and not in the same container.

Or, perhaps the base class should have a virtual function called "say", and Son1 says "hello", while Son2 says "bye" in their implementations.

_______________________________________________

As a last resort, you can cast individual objects from base class to derived class, but not the container (vector) itself.
Last edited on Apr 20, 2020 at 7:47pm
Apr 20, 2020 at 7:45pm
for the real code it would be a mess, since there are a lot of sub-classes and are quite different between them.


This suggests that perhaps they shouldn't be related. Shouldn't be sub-classes of the same parent.
Apr 20, 2020 at 7:58pm
at its simplest, a vector of any kind of pointer would do:

vector <char *> v(2);
v[0] = (char*) (new Son);
v[1] = (char*)(new Son2);

(Son*)(v[0])->sayhello();


again should I be trying to do it?
I don't really know what you want to accomplish.

There are OOP ways to do the above, but it needs more code/words/bloat to do the exact same thing.
Last edited on Apr 20, 2020 at 8:00pm
Topic archived. No new replies allowed.