Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Zoot

Class pointers

This topic is 5801 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 am playing some with classes and i am trying to make a list of all my classes: class *classlist[128] int usedclasses = 0; void ADD_CLASS(class *pClass) { classlist[usedclasses] = pClass; usedclasses++; } class CBajs { public: int test; char classname[128]; }; ADD_CLASS(CBajs); But this isn´t working... any ideas?

Share this post


Link to post
Share on other sites
Advertisement
You have to instantiate the class first. A class name is a type.

Thus,

ADD_CLASS(CFoo)

will not work cause your passing a type.

CFoo* pFoo = new CFoo;
ADD_CLASS(pFoo);

This will work.

Share this post


Link to post
Share on other sites

  
class *classlist[128];


is that even legal? (I''m away from my work PC, hence I can''t compile it to find out )

Share this post


Link to post
Share on other sites
NuffSaid, i want to create instantiate of the class later using the classlist.

Yes, LordShade class *classlist[128]; is legal.

Share this post


Link to post
Share on other sites
You may want to look up on how MFC does it with their RUNTIME_CLASS, DECLARE_DYNAMIC and DECLARE_DYNCREATE. You may find it interesting.

Share this post


Link to post
Share on other sites
quote:
Original post by Zoot
Yes, LordShade class *classlist[128]; is legal.

No, Zoot, class * classlist[128]; isn''t legal - at least, not in the sense you think.

class *classlist[128];
class T
{
public:
T() { cout << typeid( *this ).name() << endl;
};
 
int main()
{
T * t = new T;
classlist[0] = t;
return 0;
}


error C2440: ''='' : cannot convert from ''class T *'' to ''class $S1 *''
Types pointed to are unrelated; conversion requires reinterpret_cast,
C-style cast or function-style cast

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Classes are not objects of a class type, classes are types. That''s all there is to it. Now the question is, why do you want to do this?

Share this post


Link to post
Share on other sites
Derive all classes from one base class
and store them in a linked list of pointer''s to the base class

Share this post


Link to post
Share on other sites
quote:
Original post by George2
Derive all classes from one base class and store them in a linked list of pointer''s to the base class

Only do this if the classes are actually related (ie, don''t introduce inheritance and polymorphism merely for convenience of reference). It''s an obvious indication of poor design.

(It probably goes without saying that concepts and generic algorithms are beyond your ken, so I''ll save that discussion for some other time).

Share this post


Link to post
Share on other sites

  
class CBase
{
};

class CDerived1 : public CBase
{
public:
static CBase* CreateObject()
{
return new CDerived1;
}
};

class CDerived2 : public CBase
{
public:
static CBase* CreateObject()
{
return new CDerived2;
}
};

typedef CBase* (*CreateObjectFunc)();

int main()
{
CreateObjectFunc pCreateObjectFuncs[2];

pCreateObjectFuncs[0] = CDerived1::CreateObject;
pCreateObjectFuncs[1] = CDerived2::CreateObject;

CBase* pBase1 = pCreateObjectFuncs[0]();
CBase* pBase2 = pCreateObjectFuncs[1]();

return 0;
}

This could work as you intend. You could expand it further to send init-parameters to the constructors.

Share this post


Link to post
Share on other sites

  • 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!