Thread View: comp.sys.mac.oop.misc
2 messages
2 total messages
Started by Richard Jason Lyders
Sun, 14 Jan 1996 20:20
[Q] [enums] class_ID as enum is basically useless? Why not GetClassID()?
Author: Richard Jason Lyders
Date: Sun, 14 Jan 1996 20:20
Date: Sun, 14 Jan 1996 20:20
85 lines
3920 bytes
3920 bytes
HELP! Suggestions are MOST WELCOME!
Why would someone use an enum for a class_ID in a class library? Why not
simply provide a GetClassID() function for each class? That would be so
much simpler and so much more useful!
I don't seem to be able to use the enum class_ID to find out what class
an object is because class_ID isn't "virtual" like a function. If you have
an object of a class that is derived from a base class, and you are
referencing that object using the base pointer, then class_ID does not
tell you the class of the object, it tells you the class of the pointer.
What god is this? I know what class the pointer is because I cast it
myself! Duh?
This means that I don't seem to be able to create a routine that uses a
base class as a pointer to any of that base class's derived classes. . .
because in that routine I need to know exactly what the class_id is of the
REAL object rather than the class_ID of the base object (which is pretty
darn obvious because it never changes).
I did not expect to run into this problem. Is this a limitation? an error
on my part? Is there another way to find out the actual class of an object
without using a class_ID enum that really will give me the correct
class_ID?
So , by using an enum . . .whatever you type cast a pointer as. . . that's
the class ID that you get. . . so it isn't any better than a compiler
#define as far as I am trying to use it.
If you check out my code example below. . .you will see my confusion as to
why a class_ID enum would be used . . . and I hope ther is another way of
finding out an object's actual class ID.
PS: and no, it is not possible to give each class a GetClassID routine
because I am having this problem with the Metrowerks PowerPlant class
library and I don't think it is very acceptible to just go around
changing the inner workings of a class library.
class LPane {
public:
enum { class_ID = 'pane' };
LPane() {};
~LPane() {};
};
class LView : public LPane {
public:
enum { class_ID = 'view' };
LView() {};
~LView() {};
};
void main (void) {
LPane aPane;
LView aView;
LPane * aPanePtr = &aPane;
LView * aViewPtr = &aView;
LPane * aPanePtr2 = (LPane*)aViewPtr;
long aPaneClassID = aPanePtr->class_ID; // = 'pane' (obviously)
long aViewClassID = aViewPtr->class_ID; // = 'view' (obviously)
long aWhichClassID = aPanePtr2->class_ID; // = 'pane' ********* why?
**********
}
/*
?
___ o _ _ /
| o \ _ ___| |_ ___ _ _ _| | /"""\
| /| | __) | o | '_| o | |~'~|
|_|_\|_\___)_|_|_|_|_| \___| \ - /
.---------------------------------^----.
| Richard Jason Lyders Houston, Texas |
| richard@moriarty.com |
'-----------------------------OOo----oOO
|=[]=|
/_/\_\
oOO OOo
*/
[ Articles to moderate: mailto:c++-submit@netlab.cs.rpi.edu ]
[ Read the C++ FAQ: http://www.connobj.com/cpp/cppfaq.htm ]
[ Moderation policy: http://www.connobj.com/cpp/guide.htm ]
[ Comments? mailto:c++-request@netlab.cs.rpi.edu ]
Re: [Q] [enums] class_ID as enum is basically useless? Why not GetClassID()?
Author: David Reiss
Date: Mon, 15 Jan 1996 22:28
Date: Mon, 15 Jan 1996 22:28
106 lines
4927 bytes
4927 bytes
The class_ID is meant to NOT be virtual...this way it can be used to
register the stream-constructor static routines with URegistrar allowing
easy construction using Constructor. I suppose a static GetClassID()
routine would be ok, but there's no real difference there. If you
want to do a
myPane->GetClassID()
kind of call, you should probably use RTTI (as far as I've been told) to
determine what kind of object your myPane is. As far as I know, there's
no other way, other than modifying the PP code itself.
-David
richard@moriarty.com (Richard Jason Lyders) wrote:
>HELP! Suggestions are MOST WELCOME!
>
>Why would someone use an enum for a class_ID in a class library? Why not
>simply provide a GetClassID() function for each class? That would be so
>much simpler and so much more useful!
>
>I don't seem to be able to use the enum class_ID to find out what class
>an object is because class_ID isn't "virtual" like a function. If you have
>an object of a class that is derived from a base class, and you are
>referencing that object using the base pointer, then class_ID does not
>tell you the class of the object, it tells you the class of the pointer.
>What god is this? I know what class the pointer is because I cast it
>myself! Duh?
>
>This means that I don't seem to be able to create a routine that uses a
>base class as a pointer to any of that base class's derived classes. . .
>because in that routine I need to know exactly what the class_id is of the
>REAL object rather than the class_ID of the base object (which is pretty
>darn obvious because it never changes).
>
>I did not expect to run into this problem. Is this a limitation? an error
>on my part? Is there another way to find out the actual class of an object
>without using a class_ID enum that really will give me the correct
>class_ID?
>
>So , by using an enum . . .whatever you type cast a pointer as. . . that's
>the class ID that you get. . . so it isn't any better than a compiler
>#define as far as I am trying to use it.
>
>If you check out my code example below. . .you will see my confusion as to
>why a class_ID enum would be used . . . and I hope ther is another way of
>finding out an object's actual class ID.
>
>PS: and no, it is not possible to give each class a GetClassID routine
>because I am having this problem with the Metrowerks PowerPlant class
>library and I don't think it is very acceptible to just go around
>changing the inner workings of a class library.
>
>class LPane {
> public:
> enum { class_ID = 'pane' };
> LPane() {};
> ~LPane() {};
>};
>
>class LView : public LPane {
> public:
> enum { class_ID = 'view' };
> LView() {};
> ~LView() {};
>};
>
>void main (void) {
> LPane aPane;
> LView aView;
> LPane * aPanePtr = &aPane;
> LView * aViewPtr = &aView;
> LPane * aPanePtr2 = (LPane*)aViewPtr;
>
> long aPaneClassID = aPanePtr->class_ID; // = 'pane' (obviously)
> long aViewClassID = aViewPtr->class_ID; // = 'view' (obviously)
> long aWhichClassID = aPanePtr2->class_ID; // = 'pane' ********* why?
>**********
>}
>
>/*
> ?
> ___ o _ _ /
>| o \ _ ___| |_ ___ _ _ _| | /"""\
>| /| | __) | o | '_| o | |~'~|
>|_|_\|_\___)_|_|_|_|_| \___| \ - /
>.---------------------------------^----.
>| Richard Jason Lyders Houston, Texas |
>| richard@moriarty.com |
>'-----------------------------OOo----oOO
> |=[]=|
> /_/\_\
> oOO OOo
>
>*/
>
> [ Articles to moderate: mailto:c++-submit@netlab.cs.rpi.edu ]
> [ Read the C++ FAQ: http://www.connobj.com/cpp/cppfaq.htm ]
> [ Moderation policy: http://www.connobj.com/cpp/guide.htm ]
> [ Comments? mailto:c++-request@netlab.cs.rpi.edu ]
[ Articles to moderate: mailto:c++-submit@netlab.cs.rpi.edu ]
[ Read the C++ FAQ: http://www.connobj.com/cpp/cppfaq.htm ]
[ Moderation policy: http://www.connobj.com/cpp/guide.htm ]
[ Comments? mailto:c++-request@netlab.cs.rpi.edu ]
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads