• Advertisement

Archived

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

Class pointers

This topic is 5536 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
quote:
Original post by Zoot
NuffSaid, i want to create instantiate of the class later using the classlist.

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


I should have read the first post more closely. You will have to do runtime typing.

Also, I believe "class" is a keyword that ultimately compiles out to mean structure. Thus,

class* classlist[128]

is compiled out to

structure* classlist[128]

which means you don''t have a type defined so the compiler has no idea what type you are asking to store. Sure it can allocate the storage, a pointer is a pointer no matter what, but trying to assign something to it, well now, that''s when the compiler sits up and goes, "HUH!?"

Share this post


Link to post
Share on other sites
Zoot - C++ supports (currently ? Oh, please! Please! *beg* *grovel* Gimme! Please! )neither reflexion nor metaclasses. You can, however implement an abstract factory. See also this link in the C++ FAQ-Lite.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]


[edited by - Fruny on December 31, 2002 3:50:27 AM]

Share this post


Link to post
Share on other sites

  • Advertisement