我有两个简单的类。我想用矢量显示结果,但数字不显示。另一方面,当我尝试没有向量的结果时,结果将显示。你可以帮我吗?谢谢。向量显示错误
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
using namespace std;
template<typename T>
class One
{
protected:
T word;
T word2;
public:
One() {word = "0"; word2 = "0";}
One(T w, T w2) {word = w; word2 = w2;}
virtual const void Show() {cout << word << endl; cout << word2 << endl;}
};
template<typename T>
class Two : public One<T>
{
private:
int number;
public:
Two() {number = 0;}
Two(T w, T w2, int n) : One(w,w2) {number = n;}
virtual const void Show() {cout << word << endl; cout << word2 << endl; cout << number << endl; }
};
int main()
{
One<string> *idk;
Two<string> *something = new Two<string>("asd","aa",1);
idk = something;
idk->Show(); // OK - asd, aa, 1
vector<One<string>> arr;
arr.push_back(*idk);
arr.at(0).Show(); // WRONG - asd,aa
return 0;
}
请参阅[C++中的切片问题?](http://stackoverflow.com/questions/274626/what-is-the-slicing-problem-in-c/274634#274634) – WhozCraig 2013-05-04 14:20:15
':一个( w,w2){number = n; }'我想应该是':一个(w,w2){number = n;}' –
0x499602D2
2013-05-04 14:21:25