Skip to main content
gamedev.net
🔒 Locked

Class pointers

Started by Zoot 10 replies 1.1k views
Original Post
Zoot
Zoot
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?
GSACP: GameDev Society Against Crap PostingTo join: Put these lines in your signature and don't post crap!
LordShade
LordShade
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.
NuffSaid
NuffSaid

  
class *classlist[128];


is that even legal? (I''m away from my work PC, hence I can''t compile it to find out )
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
Zoot
Zoot
NuffSaid, i want to create instantiate of the class later using the classlist.

Yes, LordShade class *classlist[128]; is legal.
GSACP: GameDev Society Against Crap PostingTo join: Put these lines in your signature and don't post crap!
Oluseyi
Oluseyi
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
George2
George2
Derive all classes from one base class
and store them in a linked list of pointer''s to the base class
"THE INFORMATION CONTAINED IN THIS REPORT IS CLASSIFIED; DO NOT GO TO FOX NEWS TO READ OR OBTAIN A COPY." , the pentagon
Oluseyi
Oluseyi
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).
dalleboy
dalleboy

  
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.
LordShade
LordShade
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!?"
Fruny
Fruny
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]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.