0

I have two simple classes. I want with vector show result, but number is not showed. On the other hand, when I try result without vector, result will be show. Can you help me? Thank you.

#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;
}
Facedown
  • 123
  • 1
  • 7
  • 2
    See [What is the Slicing Problem in C++?](http://stackoverflow.com/questions/274626/what-is-the-slicing-problem-in-c/274634#274634) – WhozCraig May 04 '13 at 14:20
  • `: One(w, w2) { number = n; }` I think should be `: One(w, w2) {number = n;}` – David G May 04 '13 at 14:21

2 Answers2

3

You are storing One<string> instances instead that pointers to them One<string>*.

Since the object is stored totally in the vector cell, there is no polymorphism as the object suffers from slicing: any additional feature that inherits from One in the instance you are placing inside the vector is just discarded.

Try with a vector<One<string>*> so that a pointer is stored and the problem doesn't occur. Mind that, to manage memory, using a smart pointer when working with STL collections is a wise choice.

Jack
  • 131,802
  • 30
  • 241
  • 343
0

Your vector stores One<string> objects. When you push_back(*idk), a new One<string> object is created via a copy constructor from *idk.

If you want to keep the virtual behavior, you should use a vector of pointers: vector<One<string>*> arr; and then arr.push_back(idk);.

nullptr
  • 11,008
  • 1
  • 23
  • 18