Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


List of classes


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Retsu90   Members   -  Reputation: 208

Like
0Likes
Like

Posted 02 May 2013 - 09:13 AM

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?



Sponsor:

#2 AllEightUp   Moderators   -  Reputation: 4267

Like
1Likes
Like

Posted 02 May 2013 - 09:36 AM

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.



#3 Retsu90   Members   -  Reputation: 208

Like
0Likes
Like

Posted 03 May 2013 - 08:09 AM

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)'






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS