.:: Bots United ::.

.:: Bots United ::. (http://forums.bots-united.com/index.php)
-   General Programming (http://forums.bots-united.com/forumdisplay.php?f=25)
-   -   [C++] Class names (http://forums.bots-united.com/showthread.php?t=4389)

@$3.1415rin 24-08-2005 17:22

[C++] Class names
 
is there a possibility to get the name of a class from one of its base classes without much effort ? would be nice if it'd be somewhat standardized stuff :D

Rick 24-08-2005 17:39

Re: [C++] Class names
 
Use dynamic_cast?

sfx1999 24-08-2005 17:53

Re: [C++] Class names
 
I am not sure quite what you mean, but you could have a private variable that is modified to the class's name in the class's constructor. You could also make a global constant value and just set the string to use it.

@$3.1415rin 24-08-2005 18:06

Re: [C++] Class names
 
dynamic cast would cast it, yes, but I want the name of it.

I could of course create a virtual function returning the name, or a member variable being updated. that could of course be optimized by some preprocessor stuff to have less to write. but that's all stuff i'd call too much effort :)

Rick 24-08-2005 18:57

Re: [C++] Class names
 
Quote:

Originally Posted by @$3.1415rin
dynamic cast would cast it, yes, but I want the name of it.

I could of course create a virtual function returning the name, or a member variable being updated. that could of course be optimized by some preprocessor stuff to have less to write. but that's all stuff i'd call too much effort :)

Well...with dynamic_cast you can see if class X is from type Y. If its not it will return NULL.
Although I'm not sure if it will work on base classes...

I think the best way is(like sfx1999 said) to make a way to 'identify' each class, since dynamic_cast isn't very efficient I believe.

sfx1999 24-08-2005 19:57

Re: [C++] Class names
 
Code:

char g_mainclassname[] = "mainclass";
char g_derivedclassname[] = "derived class";

class mainclass
{
  mainclass()
  {
        name = g_mainclassname;
  };
  public:
        char *identify(){return name};
  protected:
        char *name;
};

class derivedclass:public mainclass
{
  derivedclass{ name = g_derivedclassname };
};


Whistler 25-08-2005 01:17

Re: [C++] Class names
 
you can also use #define to avoid typing the get class name function for each class:

http://cvs.sourceforge.net/viewcvs.p....1&view=markup

sfx1999 25-08-2005 01:35

Re: [C++] Class names
 
Whistler, I think what he means is that he wants the base class to be able to find out what the derived class is. Like this:

Code:

CBaseClass *instance = new CDerivedClass;
instance.identify();

If you use your method, wouldn't it always return the main class's name?

sfx1999 25-08-2005 02:26

Re: [C++] Class names
 
I just realized something. My method might not work either. The main class's constructor might override the derived one's. You would need a hack to check if the value was set.

Whistler 25-08-2005 03:52

Re: [C++] Class names
 
Quote:

Originally Posted by sfx1999
Whistler, I think what he means is that he wants the base class to be able to find out what the derived class is. Like this:

Code:

CBaseClass *instance = new CDerivedClass;
instance.identify();

If you use your method, wouldn't it always return the main class's name?

the main class's "GetDescription()" is virtual, so it should return the derived class's name.


All times are GMT +2. The time now is 06:38.

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.