我希望看到来自两个不同派生类的两个对象打印,关键是在定义为基类后将对象强制转换为派生类。这是代码:
#include <iostream>
#include "memory"
using namespace std;
template<class T>
class Base
{
public:
Base() {};
~Base() {};
void Print()
{
std::cout << "Print from Base" << std::endl;
}
};
class Derived1: public Base<int>
{
public:
Derived1() {}
void Print()
{
std::cout << "Print from Derived1" << std::endl;
}
};
class Derived2: public Base<int>
{
public:
Derived2() {}
void Print()
{
std::cout << "Print from Derived2"<< std::endl;
}
};
class user
{
public:
user();
user(int type)
:m_type(type)
{
switch(type) // change should be done here?
{
case 1:
m_ptr = make_unique<Derived1>(); // m_ptr still base? why not derived
break;
case 2:
m_ptr = make_unique<Derived2>();;
break;
default:
break;
}
}
~user() {};
void UserPrint()
{
m_ptr->Print();
}
private:
std::unique_ptr<Base<int>> m_ptr;
int m_type;
};
int main()
{
user a(1);
user b(2);
a.UserPrint();
b.UserPrint();
return 0;
}
我期待看到
Print from Derived1
Print from Derived2
但相反,我看到了
Print from Base
Print from Base
如何更改代码以使其正常工作?我更愿意更改
user(int type)
的构造函数。
我认为您对 C++ 中的重写方法有一些误解,因为您缺少(假定的)虚拟方法上的 virtual 关键字:
#include <iostream>
#include "memory"
using namespace std;
template<class T>
class Base
{
public:
Base() {};
~Base() {};
virtual void Print()
{
std::cout << "Print from Base" << std::endl;
}
};
class Derived1: public Base<int>
{
public:
Derived1() {}
void Print() override
{
std::cout << "Print from Derived1" << std::endl;
}
};
class Derived2: public Base<int>
{
public:
Derived2() {}
void Print() override
{
std::cout << "Print from Derived2"<< std::endl;
}
};
class user
{
public:
user();
user(int type)
:m_type(type)
{
switch(type) // change should be done here?
{
case 1:
m_ptr = make_unique<Derived1>(); // m_ptr still base? why not derived
break;
case 2:
m_ptr = make_unique<Derived2>();;
break;
default:
break;
}
}
~user() {};
void UserPrint()
{
m_ptr->Print();
}
private:
std::unique_ptr<Base<int>> m_ptr;
int m_type;
};
int main()
{
user a(1);
user b(2);
a.UserPrint();
b.UserPrint();
return 0;
}