0

Here is the problem. I have vector<D2*>, where D2 is a Parent.

I add there childs: D3 and D4.

void readFromInput(std::vector<D2*>& vec) {
        std::cout << "\nSecond class input";
        int x = 0, y = 0, z = 0;
        std::cout << "\nEnter x: ";
        std::cin >> x;
        std::cout << "Enter y: ";
        std::cin >> y;
        std::cout << "Enter z: ";
        std::cin >> z;
        vec.push_back(new D3(x, y, z));

    }


void readFromInput(std::vector<D2*>& vec) {
        std::cout << "\nSecond class input";
        int x = 0, y = 0, z = 0;
        std::cout << "\nEnter x: ";
        std::cin >> x;
        std::cout << "Enter y: ";
        std::cin >> y;
        std::cout << "Enter z: ";
        std::cin >> z;
        vec.push_back(new D4(x, y, z));

    }

And here is how i output it.

void output(std::vector<D2*>& vec) {

    for (const auto& item : vec) {
        item->display();
        std::cout << " | ";
    }

}

display() is a virtual function, btw.

So, when i use "output" i see every single element in this vector, of course.

Is there any method to ignore, for example, elements from D3 or D4?

I mean when vec.push_back(new D3(1, 1, 1)), vec.push_back(new D4(2, 2, 2))

My ouput will be 1 1 1 | 2 2 2

Can it be 1 1 1| or 2 2 2| , using the same function?

acidx27x
  • 13
  • 2
  • You could provide another virtual function doing some check, returning true or false depending on you wanting to print, you could check the runtime type (rtti), or you could do a `dynamic_cast` to `Dn*` (if wanting to skip `Dn`, then you skip if you don't get a null pointer). – Aconcagua Nov 23 '21 at 11:36
  • [Visitor pattern](https://en.wikipedia.org/wiki/Visitor_pattern) as another alternative... – Aconcagua Nov 23 '21 at 11:39
  • It looks like you have made the "a higher-dimensional vector is a kind of lower-dimensional vector" mistake. – molbdnilo Nov 23 '21 at 11:49
  • @Aconcagua, could you, please, show how to do dynamic_cast? – acidx27x Nov 23 '21 at 12:18
  • `auto d4 = dynamic_cast(vec[someIndex]);` – it's a rather slow mechanism, though. Having a good design you usually don't need to fall back to dynamic casts, so maybe you should rather rethink about the latter. Are we possibly talking about an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)? – Aconcagua Nov 23 '21 at 12:23
  • @Aconcagua, thank you so much, this works for me!! No, its not a XY problem. – acidx27x Nov 23 '21 at 12:37

1 Answers1

0

For your particular case you can use dynamic_cast<T>(expression). See some other stack overflow answers

  1. dynamic_cast and static_cast in C++
  2. When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used?

You can do something like this

  void output(std::vector<D2*>& vec) {
    
        for (const auto& item : vec) {
            auto ptr = dynamic_cast<D3*>(item)
            if(ptr != nullptr){
               ptr->display();
             }else{/*Do nonthing*/}
            std::cout << " | ";
        }
    
    }

Please note that this is a bad practice!

If you want this kind of functionality you should re-think that design of your program.

kostakis
  • 121
  • 1
  • 3