I am trying to find a way to access a function of the derived class through the base pointer without dynamic casting. I have tried the visitor pattern as suggested in this post, but it seems like it does not work for templated Derived class. Here is what I have:
#include <iostream>
#include <memory>
class Base
{
public:
virtual print() = 0;
};
template<class T>
class Derived final : public Base
{
private:
T value;
public:
Derived() = default;
explicit Derived(const T& data) : value{data} {}
T get_value () {return this->value;}
void print() {std::cout << value << std::endl;}
~Derived() {}
};
int main()
{
std::shared_ptr<Base> ptr_one = std::make_shared<Derived<int>>(3);
std::shared_ptr<Base> ptr_two = std::make_shared<Derived<int>>(3);
auto value = ptr_one->get_value(); // This will cause an error.
auto value_2 = ptr_two->get_value() // This will cause an error.
std::cout << value == value_2 << std::endl; // This is my final goal. Being able to compare the underlying data.
return 0;
}
My final goal is being able to compare the underlying data of two instances of the Derived class. Is there any way to achieve such task?