DECLARE_CLASS Macro

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • DECLARE_CLASS Macro

      Guys,

      Have any of you ran across this macro (I think its a macro): "DECLARE_CLASS(...)"

      When I did a google, I got nothing that helps explain what its for or why even have it. Here's how I've seen it used:

      Source Code

      1. class myClass : public IClass
      2. {
      3. DECLARE_CLASS(myClass);
      4. pubilc:
      5. ...
      6. };



      Me
    • Where is it being used? And to what end does it appear to be being used?
      Feel you safe and secure in the protection of your pants . . . but one day, one day there shall be a No Pants Day and that shall be the harbinger of your undoing . . .
    • There is something similar to that in the Source SDK.

      From c_baseentity.h in the SourceSDK

      Brainfuck Source Code

      1. //-----------------------------------------------------------------------------
      2. // Purpose: Base client side entity object
      3. //-----------------------------------------------------------------------------
      4. class C_BaseEntity : public IClientEntity
      5. {
      6. // Construction
      7. DECLARE_CLASS_NOBASE( C_BaseEntity );
      8. friend class CPrediction;
      9. public:
      10. // etc ..
      Display All


      In classes which you inherit from C_BaseEntity you should call the macro DECLARE_CLASS( myClass , myParentClass ) and this macro typedefs myClass and myParentClass correctly so you can use further marcos which have been specified in terms of the typedefs.

      And this is the reason I don't like MACROs. They could be doing anything. Confusing as hell to deal with and your macro is probably doing something completely different.

      Colm Mac
    • I guess I'm not sure what you mean. Can you explain it more clearly, please? One question is 'why' do such a thing? Whats the point?

      From what your saying I can do something like this;

      Source Code

      1. #define DECLARE_CLASS( someClass ) \
      2. typedef someClass [?????];

      The above code will not work. What do I typedef against?
      -----------------------------------------------------------------------------

      Now if I have an extra parameter like: DECLARE_CLASS( Class1, Class2 )

      Source Code

      1. #define DECLARE_CLASS( someClass, someOtherClass )
      2. typedef someClass someOtherClass;
      3. class Class1 : public Class2
      4. {
      5. DECLARE_CLASS( Class1, Class2 )
      6. public:
      7. ...
      8. };


      That to me does not make much sence, why would you do this, whats the point? Since I've been googling this, is there a term for this technique?

      S.
    • We do something like this for our property system to declare a bunch of generic functions for each property without having to do a cast at run-time.

      Source Code

      1. #define DECLARE_PROPERTY(propertyClass) \
      2. public: \
      3. static string GetPropertyId(); \
      4. static propertyClass* Get(const Entity* pEnt) { return (propertyClass*)(pEnt?pEnt->GetProperty(propertyClass::GetPropertyId()):NULL); } \
      5. static propertyClass* Create(Entity* pEnt) { return (propertyClass*)(pEnt?pEnt->AddProperty(propertyClass::GetPropertyId()):NULL); } \
      6. static propertyClass* Create(string& entNameId) { return Create(Entity::Create(entNameId)); } \
      7. static propertyClass* Get(Property* pSib) { return pSib?Get(pSib->GetOwner()):NULL; } \
      8. static propertyClass* Get(const string& s) { return Get(Entity::Get(s)); } \
      9. static propertyClass* Get(const char* sz) { return Get(Entity::Get(sz)); } \
      10. static propertyClass* Get(const EntityId& id) { return Get( id.Get() ); } \
      11. virtual StringId GetPropertyImplId() { return propertyClass::GetPropertyId(); }
      Display All

      And when you create a property.....

      Source Code

      1. class SomeProperty : public Property
      2. {
      3. DECLARE_PROPERTY(SomeProperty);
      4. // imagine fun declarations here
      5. };


      Based on your original post, my guess is that DECLARE_CLASS is doing something similar.

      -Rez
    • Right the thing to remember is that you are not using this macro in isolation.

      So DECLARE_CLASS is

      Source Code

      1. #define DECLARE_CLASS( someClass ) \
      2. typedef someClass a_declared_class;


      And you have a whole bunch of other macros that set up a class factory and make the class do network serialisation.

      Source Code

      1. #define DECLARE_SERIALIZABLE()\
      2. public:\
      3. a_declared_class( char* c_string );\
      4. a_declared_class( std::string& a_string );\
      5. \
      6. char* GetAsCString() const;\
      7. std::string GetAsSTDString() const;\
      8. \
      9. private:\
      10. std::string cacheOfSerialization;


      And we'd also like the class to be deep copyable (not use the compiler genater copy constructor or copy assignement.

      Source Code

      1. #define DECLARE_DEEP_COPYABLE() \
      2. public:\
      3. a_declared_class( const a_declared_class& anotherClass );\
      4. a_declared_class& operator=(const a_declared_class& rhs) ;


      Then went we want to create a new class which is serializable we can do

      Source Code

      1. class CMacroFilled {
      2. DECLARE_CLASS( CMacroFilled );
      3. DECLARE_DEEP_COPYABLE();
      4. DECLARE_SERIALIZABLE();
      5. }


      And if you have hundreds of classes that are all meant to be deep copied or serializable then you have saved yourself a lot of typing. And if you make errors then you only have to fix it in one place.

      Another plus to doing this is that the compilier will tell you if you haved not implemented a function that is required. This method can catch some particularly insidious bugs that can happen if you don't overload a function you are meant to.

      EDIT: Rez got in there while I writing. A property system is a good reason to use MACROs. It basically just save a lot of typing. And oh yeah I don't know if this technique has a name. Can anyone enlighten me?

      The post was edited 2 times, last by Colm Mac ().

    • RE: DECLARE_CLASS Macro

      ?(

      In the second example I have, it would seems somewhat dumb and confusing. Why would you want to do this?
      (Why would I typedef a class??) I can understand this:

      "typedef unsigned int UINT;"

      But why would I want to typedef a class name into another name?

      Also, I found this intresting macro.

      Source Code

      1. typedef baseClassName BaseClass; \
      2. typedef className ThisClass; \
      3. public: \
      4. static char const *GetClassName() { return #className; } \
      5. static char const *GetlBaseClassName() { return #baseClassName; }


      I think it only returns the name of the class, any ideas on why you would want to get the class name during execution? (I don't think this is the entire macro.)

      Sabrina, very confused and VERY dazzed.


      (I did check MSDN prior to asking here, they were no help.)
    • In the second example you would typedef your class to [????] because further macros have been written for a class called [????] and the complier is informed that at that time [????] is an alias for someClass.

      The thing to remember here is that typedefs remain until the compiler comes across another typedef for [????].

      Typedefs in general are used for setting up aliases. I don't generally typedef classes but I do like to set up typedefs for templated classes and STL containers.


      As for your interesting macros which returns a classname. The compiler RTTI (Run Time Type Identification) was introduced to allow you to used the dynamic_cast. In general dynamic_cast is expensive and you'd want to avoid using it if you can. So a custom RTTI might be faster.
    • Colm Mac,

      Hey thanks. I do however feel that its greek or Hindu to me. I'll just have to put it onthe shelf for now and think about it. If you [guys] have a small and simple program that will demostrate the concept, send it my way. (K.I.S.S. please)

      I have come across the RTTI thingy, and several articals that I've read also mentioned that is not a good idea to do/use. (Which is fine for me :) )

      S.
    • My general view (and certainly what I've read in things like Scott Meyers) is that macros and #defines should be avoided in normal use. I don't normally create them for anything except to avoid typing the same thing repeatedly. I didn't write any except the odd ( #define PI 3.1416 ) in my first four or five years of coding.

      Most of the functionality they provide has been superceded by thing like templates and static const values. I now write "static const float PI 3.1416f" for example.

      You only need to be aware that they exist.

      By the way if you haven't read Scott Meyers' Effective C++ yet, you should get around to it.
    • Oh yea, I forgot what Meyer says about using MACRO's.

      You guys (programmers) do some weird stuff sometimes and it leaves us (amature's/novice's) scratching our heads...STOP IT!

      Now that I got that out of my system, let me just say that I gotta admit you guys keep me keep me on my feet and you keep me thinking, thanks :)

      Me.
      "There is a method to my madness."