[ create a new paste ] login | about

Link: http://codepad.org/2It3PLSN    [ raw code | fork ]

C++, pasted on Jul 25:
#include <vector>
#include <assert.h>

class IB
{
public:
	virtual void f() = 0;

};

template  <typename T>class A
{
	std::vector<IB*> myObj;

public:

	
	void add(IB* anObj)
	{
		myObj.push_back(anObj);
	}

	void get(int anIndex, T*& anObj)
	{
		T* t = dynamic_cast<T*>(myObj[anIndex]);
		anObj = t;
	}
};

class C : public IB
{
	public:
	virtual void f()
	{
	}
};
class D : public C
{
	public:
	virtual void f()
	{
	}
};

class E : public IB
{
	public:
	virtual void f()
	{
	}
};

int main() 
{
	A<C> a;
	D* d = new D;
	a.add(d);
	E* e = new E;
	a.add(e);
	C* c;
	a.get(0, c);
	assert(c != NULL);
	a.get(1, c);
	assert(c == NULL);
	return 0;
}


Output:
No errors or program output.


Create a new paste based on this one


Comments: