.:: Bots United ::.  
filebase forums discord server github wiki web
cubebot epodbot fritzbot gravebot grogbot hpbbot ivpbot jkbotti joebot
meanmod podbotmm racc rcbot realbot sandbot shrikebot soulfathermaps yapb

Go Back   .:: Bots United ::. > Developer's Farm > General Programming
General Programming Help others and get yourself helped here!

Reply
 
Thread Tools
[C++] Class names
Old
  (#1)
@$3.1415rin
Council Member, Author of JoeBOT
 
@$3.1415rin's Avatar
 
Status: Offline
Posts: 1,381
Join Date: Nov 2003
Location: Germany
Default [C++] Class names - 24-08-2005

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


  
Reply With Quote
Re: [C++] Class names
Old
  (#2)
Rick
Council Member
 
Rick's Avatar
 
Status: Offline
Posts: 690
Join Date: Dec 2003
Location: Holland
Default Re: [C++] Class names - 24-08-2005

Use dynamic_cast?
  
Reply With Quote
Re: [C++] Class names
Old
  (#3)
sfx1999
Member
 
sfx1999's Avatar
 
Status: Offline
Posts: 534
Join Date: Jan 2004
Location: Pittsburgh, PA, USA
Default Re: [C++] Class names - 24-08-2005

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.


sfx1999.postcount++

Last edited by sfx1999; 24-08-2005 at 17:55..
  
Reply With Quote
Re: [C++] Class names
Old
  (#4)
@$3.1415rin
Council Member, Author of JoeBOT
 
@$3.1415rin's Avatar
 
Status: Offline
Posts: 1,381
Join Date: Nov 2003
Location: Germany
Default Re: [C++] Class names - 24-08-2005

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


  
Reply With Quote
Re: [C++] Class names
Old
  (#5)
Rick
Council Member
 
Rick's Avatar
 
Status: Offline
Posts: 690
Join Date: Dec 2003
Location: Holland
Default Re: [C++] Class names - 24-08-2005

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.
  
Reply With Quote
Re: [C++] Class names
Old
  (#6)
sfx1999
Member
 
sfx1999's Avatar
 
Status: Offline
Posts: 534
Join Date: Jan 2004
Location: Pittsburgh, PA, USA
Default Re: [C++] Class names - 24-08-2005

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 };
};


sfx1999.postcount++
  
Reply With Quote
Re: [C++] Class names
Old
  (#7)
Whistler
Summoner
 
Whistler's Avatar
 
Status: Offline
Posts: 1,499
Join Date: Feb 2004
Location: Mist Village
Default Re: [C++] Class names - 25-08-2005

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

Last edited by Whistler; 25-08-2005 at 01:19..
  
Reply With Quote
Re: [C++] Class names
Old
  (#8)
sfx1999
Member
 
sfx1999's Avatar
 
Status: Offline
Posts: 534
Join Date: Jan 2004
Location: Pittsburgh, PA, USA
Default Re: [C++] Class names - 25-08-2005

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.postcount++
  
Reply With Quote
Re: [C++] Class names
Old
  (#9)
sfx1999
Member
 
sfx1999's Avatar
 
Status: Offline
Posts: 534
Join Date: Jan 2004
Location: Pittsburgh, PA, USA
Default Re: [C++] Class names - 25-08-2005

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.


sfx1999.postcount++
  
Reply With Quote
Re: [C++] Class names
Old
  (#10)
Whistler
Summoner
 
Whistler's Avatar
 
Status: Offline
Posts: 1,499
Join Date: Feb 2004
Location: Mist Village
Default Re: [C++] Class names - 25-08-2005

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.
  
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
vBulletin Skin developed by: vBStyles.com