-2

I am trying to figure out how to do oop in C++...

The issue lies with overrides. Consider the following code:

#include <iostream>

class Base {
public:

    Base() { }
    virtual void print() { 
        std::cout << "Base" << std::endl; 
    }

};

class Derrived: public Base {
public:

    Derrived() : Base() { }
    void print() override { 
        std::cout << "Derrived" << std::endl; 
    }

};


int main() {
    std::cout << "Hello, World!" << std::endl;

    Base b = Derrived();
    b.print();

    return 0;
}

When I run this program I would expect to see "Derived"! But I see "Base". So I would love to see this be possible in C++, as it is in languages like Scala, Java, C#, ... (event Rust!), but I am not sure if this is at all possible in C++.

If not maybe somebody can help me with the following situation:

std::string Value::any_to_string(std::any any) {

    std::string s = NO_VALUE;

    if (any.type() == typeid(Nothing))
        s = std::any_cast<Nothing>(any).to_string();
    if (any.type() == typeid(Boolean))
        s = std::any_cast<Boolean>(any).to_string();
    if (any.type() == typeid(Number))
        s = std::any_cast<Number>(any).to_string();
    if (any.type() == typeid(Lambda))
        s = std::any_cast<Lambda>(any).to_string();
    if (any.type() == typeid(Add))
        s = std::any_cast<Add>(any).to_string();
    if (any.type() == typeid(LiteralSet))
        s = std::any_cast<LiteralSet>(any).to_string();

    return s;
}

All of the above types inherit from a class named Value and override the method to_string(), though when casting the objects to a Value object, the definition in Value is called, rather than the one in the concrete type...

Thanks for helping me out!

Plegeus
  • 139
  • 11
  • 6
    `Base* b = new Derived();`. Also, Google "Slicing C++" – Jeffrey Sep 17 '22 at 21:02
  • 2
    I would recommend some quality reading to start your c++ journey. It doesn’t hide things from you like the more higher abstraction languages you mentioned. – Taekahn Sep 17 '22 at 21:07
  • 1
    In your "following situation" code, I don't see anything corresponding to "castingthe objects to a Value object". But in the event that you are: `any_cast` returns by value, which runs into slicing. If you only have a finite set of types you need to have in your container, you might want `std::variant`. Since they all derive from the same base class, perhaps `std::unique_ptr`. – Nathan Pierson Sep 17 '22 at 21:17
  • one of the other issues I have, is that I must work with the any type since I am using a library which throws this in my face... any concrete examples on how to get this done would be appreciated, been googling all day and can't find a lot. But I will look into the slicing stuff, my IDE already tells me the cast "looses some bytes" – Plegeus Sep 17 '22 at 21:23
  • JaMiT, I just saw your comment, it is exactly what I needed, thanks! – Plegeus Sep 17 '22 at 21:27

1 Answers1

0

Changing the main function to the following resolves the issue!

int main() {
    std::cout << "Hello, World!" << std::endl;

    Derrived derrived;
    Base* b = &derrived;
    b->print();

    return 0;
}

this works too!

int main() {
    std::cout << "Hello, World!" << std::endl;

    Base* b = new Derrived();
    b->print();

    return 0;
}
Plegeus
  • 139
  • 11