a pseudo code question

Started by
6 comments, last by luasitdown 18 years, 8 months ago
a pseudo code illustrate the interface below: for (every object in world) if (object implement the interface) interface *if; ojbect.getInterface(if); my question: I know I can store same type object or base pointer in vector. how to store the different object?
Advertisement
This is called polymorphism. You can store all objects that derive from the same base class in the same collection. You have to derive all classes from a base class.


for example, to create an object do this

classBase* pBase = new classDerived;

and have a collection of base pointers.

ace
To do that, you would have to create a vector or array type that can hold different types, but there are probably better ways to do what you want to do.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
You could look into boost::any, but you probably don't actually want to store different declared types in the same vector.
enum Bool { True, False, FileNotFound };
Okay..Templates can help you do what (I think) you want to do.

You can't store a templated class directly within a std container so you need to create a base interface class for the templates. Initially you might not want know what pure virtual functions you'd need, if any, so we can make a pure virtual destructor:

IBaseClass.h
IBaseClass{   public:      virtual ~IBaseClass() = 0; // Pure virtual function so we can't                                 // instansiate objects directly from this class                                 // it is purely as a base class so we can store                                 // derived template classes in std containers};


We need to provide some kind of implementation for this as it will be called when derived classes are destroyed (as it's virtual) so we need the following:

IBaseClass.cpp:
IBaseClass::~IBassClass{   // Don't need anything in this BUT if we don't have this minimal        // implementation we'll get linker errors (undefined references)}


Ok, so now we want some kind of template class to store our actual classes data:

ClassTemplate.h:
template <class T>class TClass : public IBaseClass{   T* Obj;   public:     TClass(const T& aObj) : Obj (new T(aObj){}     TClass(const T* aObj) : Obj(new T(*aObj)){}     virtual ~TClass(){delete Obj;}     T* operator->(){return Obj;}     T& operator.(){return *Obj;}};


ClassTemplate is basically a resource holder for your classes you want to store in the container. To create a class with this you need to declare your classes as:

AClass.h
AClass : public TClass<AClass>{   /* AClass stuff.....Constructors, VIRTUAL destructor etc...*/  }


Now you can create std containers of IBaseClass types (or pointers to them) and add derived classes on to them.

The stuff here is missing lots, such as smart pointers (boost::shared_ptr) in the template resource class, and this is very intrusive but hopefully it might give you some ideas..:)

[Edited by - garyfletcher on August 14, 2005 1:33:02 PM]
Gary.Goodbye, and thanks for all the fish.
You mean I can use template below:

AClass : public TClass<AClass>
{
AClass(AClass* a):TClass(a){}
}?
TClass mainly do some automatic assignment?
Quote:
You mean I can use template below:

AClass : public TClass<AClass>
{
AClass(AClass* a):TClass(a){}
}?
TClass mainly do some automatic assignment?


No...just create objects of AClass as normal so if your class looked like this:

AClass : public TClass<AClass>{   public:     AClass() : anInt(0)     {}     AClass(int i) : anInt(i)      {}     int getInt(){return anInt;}   private:     int anInt;}


You'd instansiate and object in the same way:

AClass myClass(3);

OR

AClass myClass = new AClass(3);

and use it as normal:

myClass.getInt();

OR

myClass->getInt();

You but now you can create a vector of different Class types as long as they inherit from TClass (which inherits from IBaseClass) so:

AClass : public TClass<AClass>{   .....stuff.....}BClass : public TClass<BClass>{   .....stuff.....}std::vector<IBaseClass*> classVec;AClass aClass = new AClass();BClass bClass = new BClass();classVec.push_back(aClass);classVec.push_pack(bClass);


:)

Gary.Goodbye, and thanks for all the fish.

AClass : public TClass<AClass>{   public:     AClass() : anInt(0)     {}     AClass(int i) : anInt(i)      {}     int getInt(){return anInt;}   private:     int anInt;}



but TClass has no default constructor
so how could you do that ?

This topic is closed to new replies.

Advertisement