Jump to content
  • Advertisement
Sign in to follow this  
luasitdown

a pseudo code question

This topic is 4838 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
You could look into boost::any, but you probably don't actually want to store different declared types in the same vector.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
You mean I can use template below:

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

Share this post


Link to post
Share on other sites
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);



:)

Share this post


Link to post
Share on other sites



AClass : public TClass<AClass>
{
public:
AClass() : anInt(0)
{}
AClass(int i) : anInt(i)
{}

int getInt(){return anInt;}

private:
int anInt;
}





[/quote]

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!