0

I got this

#include <vector>
using namespace std;

class A{
     protected:
         int test;
     public:
         void test(){} 
};
class B : public class A{
    public:
        void test2(){}
};

int main(){
     vector <A> new_vector;
     A a1;
     new_vector.push_back(a1);
     B b1;
     new_vector.push_back(b1);

     new_vector[0].test();
     //THE PROBLEM IS HOW DO I DO THIS:
     new_vector[1].test2();

}

logically is possible but it gives me an error, how I do it in the right way?

In python I do something like that all the time in a very easy way, it is possible in c++?

2
  • 3
    BTW, You are having object slice issue.
    – billz
    Aug 3, 2013 at 4:04
  • On that note, there's nothing polymorphic about this either.
    – WhozCraig
    Aug 3, 2013 at 4:08

2 Answers 2

1

Your problem is that the vector only includes space for an A object, there's no way to fit a B in there.

For polymorphism, you need a pointer, reference, or smart pointer. To help with lifetime management the latter is suggested.

std::vector<std::unique_ptr<A>> new_vector;
new_vector.emplace_back(new A());
new_vector.emplace_back(new B());

then you can use dynamic_cast on elements to discover whether there really is a B and call B specific members.

However, using virtual member functions for polymorphic behavior is much more efficient than dynamic_cast.

1
  • I wrote what you said and I've got this: "Symbol 'unique_ptr' could not be resolved" I am currently using eclipse and linux c++ compiler
    – newtonis
    Aug 3, 2013 at 12:45
0

You can do this the same way that python does it, by not storing the objects directly into the vector and instead using a pointer/reference to the actual objects themselves.

#include <memory>
typedef std::vector<std::unique_ptr<A>> AVector;

Polymorphism is allowed in this case, you can push a pointer to anything derived from A.

In the case you were trying to attempt, you were trying to fit round pegs into square holes. A std::vector actually is actually a simple wrapper around code that allocates a big block of memory members * sizeof(T).

"unique_ptr" is a C++11 container for a pointer, that knows to delete it when it goes away. If you don't have C++11/C++0x support, you could either use a pointer or make your own "auto" pointer wrapper.

// Old-style pointer
typedef std::vector<A*> AVector;
AVector avec;

avec.push_back(new A);
avec.push_back(new B);
avec.push_back(new A);
avec.push_back(new B);

// now it's your responsibility to 'delete' these allocations when you remove them.
void discardAvec(size_t position) {
    A* ptr = avec[position];
    delete ptr;
    avec.erase(avec.begin() + position);
}

See live demo of the walk-thru below at ideone.com:

#include <iostream>
#include <memory>
#include <vector>

typedef std::vector<class A*> AVector;

class A
{
protected: // so B can access it.
    int m_i;
public:
    A(int i_) : m_i(i_)
    {
        std::cout << "CTor'd A(i) " << (void*)this
                  << " with " << m_i << std::endl;
    }
    A(int i_, bool) : m_i(i_)
    {
        std::cout << "CTor'd A(i, b) " << (void*)this
                  << " with " << m_i << std::endl;

    }

    virtual ~A()
    {
        std::cout << "DTor'd A " << (void*)this << " with " << m_i << std::endl;
    }
};

class B : public A
{
    int m_j;
public:
    B(int i_, int j_) : A(i_, true), m_j(j_)
    {
        std::cout << "CTor'd B(i, j) " << (void*)this
                  << " with " << m_i << ", " << m_j << std::endl;
    }

    virtual ~B()
    {
        std::cout << "DTor'd B " << (void*)this
                  << " with " << m_i << ", " << m_j << std::endl;
    }
};

int main()
{
    AVector avec;

    std::cout << "create A(1)" << std::endl;
    avec.push_back(new A(1)); // allocated an "A" on the heap.
    std::cout << "create B(2, 1)" << std::endl;
    avec.push_back(new B(2, 1)); // allocated a "B" on the heap.
    std::cout << "create B(2, 2)" << std::endl;
    avec.push_back(new B(2, 2));
    std::cout << "create A(3) " << std::endl;
    avec.push_back(new A(3));
    std::cout << "populated avec" << std::endl;

    A* ptr = avec[2]; // take the pointer of what is actually a B
    avec.erase(avec.begin() + 2); // remove it from the vector.
    std::cout << "removed entry 2 from the vector" << std::endl;
    // 'ptr' is still valid because it's an allocation, and C++ doesn't
    // garbage collect heap allocations. We have to 'delete' it ourselves.
    // Also note that because A and B have virtual destructors,
    // you will see both of them called.
    delete ptr;

    // Now watch what DOESN'T happen as we exit.
    // everything we CTOR'd that doesn't get DTORd is a leak.
    return 0;
}
3
  • I wrote what you said and I've got this: "Symbol 'unique_ptr' could not be resolved" I am currently using eclipse and linux c++ compiler
    – newtonis
    Aug 3, 2013 at 12:31
  • It needs #include <memory> and C++0x or C++11 support. If you don't have those, then you can instead use just a traditional pointer, but then you have to make sure that you delete the objects later. See e.g. I added to the answer
    – kfsone
    Aug 3, 2013 at 16:55
  • Added a fully fleshed out demo.
    – kfsone
    Aug 3, 2013 at 17:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.