我有兩個簡單的類。我想用矢量顯示結果,但數字不顯示。另一方面,當我嘗試沒有向量的結果時,結果將顯示。你可以幫我嗎?謝謝。向量顯示錯誤
#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