Jump to content
  • Advertisement
Sign in to follow this  
Xeeynamo

List of classes

This topic is 1967 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

I know that maybe it's not the proper title, but I have some difficulties to understand how to explain the problem in a short line.

Basically I'm creating a framework with an high level of abstraction. I'm searching a decent way to create an initialization part where I describe how every object should works and a way to get that description to create the object from the index specified. I need to create some complex objects with the same structure but with different attributes. I have found a way but it seems not efficient:

#include <stdio.h>

class IObject
{
public:
    virtual IObject* Create() = 0;
    virtual void Main() = 0;
};

class A : public IObject
{
public:
    IObject* Create()
    {
        return new A;
    }
    void Main()
    {
        printf("A");
    }
};

class B : public IObject
{
public:
    IObject* Create()
    {
        return new B;
    }
    void Main()
    {
        printf("B");
    }
};

size_t objCount = 0;
IObject* objList[0x10];

void AddObject(IObject* o)
{
    objList[objCount++] = o;
}
void RemoveAllObjects()
{
    while(objCount)
        delete objList[--objCount];
}
IObject* CreateObject(int index)
{
    return objList[index]->Create();
}

void Main()
{
    IObject* a = CreateObject(0);
    IObject* b = CreateObject(1);

    a->Main();
    b->Main();

    delete a;
    delete b;
}

int main()
{
    AddObject(new A);
    AddObject(new B);

    Main();

    RemoveAllObjects();
}

 

AddObject add the object description and associate it with an index, CreateObject should create it. It works, but the problem is that with AddObject I'm creating two objects that they will be never used. I'd like to to something like AddObject(A), AddObject(B) without to create them, next create for the first time for example the object B calling CreateObject(1), without to have a startup object where to take informations about how to use the memory. I want it totally dynamic, I won't use a switch(index) { case 0: return new A; etc. 

So, it's possible to create something like a Class list instead of an object list?

Share this post


Link to post
Share on other sites
Advertisement
Guest Hiwas

Assuming the classes in question do indeed have a shared interface or base class, you can always just use std::function types in an array (TR1 or Cxx11):

 

typedef std::function< IObject* () >   Creator_t;
typedef std::vector< Creator_t >  RegisteredObjects_t;
 
RegisteredObjects_t  factory;
 
// In pre-cxx11, requires a static Create function in the derived class.
factory.push_back( &A::Create );
 
// In cxx11, you can just use a simple lambda.
factor.push_back( ()[] {return new A();} );

 

I may have the syntax a bit messed up, but it should be pretty close.

Share this post


Link to post
Share on other sites

That code doesn't work because I'm using C++03. 

Meanwhile I tried to find a solution and seems that finally I found it!

http://pastebin.com/Ngntpevu

I copied the pointer of A::Create and B::Create in the array and I call them when necessary. But the problems doesn't ends here, this works with MinGW compiler, but not with MVCPP one. The error is C2440: 'IObject *(__thiscall A::* )(void)' to 'IObject *(__thiscall *)(void)'

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!