Handy Hint or Shandy Shint...?

Started by
11 comments, last by LiquidNRG 20 years, 5 months ago
quote:Original post by YourOtherLeft
quote:Original post by superpig
quote:Original post by YourOtherLeft

void Set##var(const type &val) { this->val ;}


Uhm... how exactly does that work?


When you use DEFINE_VAR(Name) you will see in the definition that the protected member is called ''var'' the ##var tells the compiler that when the is replaced ie GetName() it looks at the variable defined Name. Not sure how better to describe it, any offers?


No, no, the token-pasting stuff is fine - I use it all the time - but when you actually use the macro, looks what happens:

//here''s the original macro#define DEFINE_VARIABLE(type,var)    public:        type Get##var() const { return var; }        void Set##var(const type &val) { this->val ;}           protected:                                                          type var;//here''s me using itDEFINE_VARIABLE(int, Foo)//here''s the output from the preprocessor    public:        int GetFoo() const { return foo; }       void SetFoo(const int &val) { this->val; }    protected:        int foo;


Now please tell me, what does the SetFoo function actually do, hmm?

Also, are you sure you don''t have to use the token-pasting operator a bit more? I''d probably write that macro like:

#define DEFINE_VARIABLE(type,var)    public:        type Get##var##() const { return ##var##; }        void Set##var##(const type &val) { this->val ;}           protected:                                                          type var##;


I''m not sure what the rules are about trailing characters on tokens in macros...

Richard "Superpig" Fine
- saving pigs from untimely fates, and when he''s not doing that, runs The Binary Refinery.
Enginuity1 | Enginuity2 | Enginuity3 | Enginuity4
ry. .ibu cy. .y''ybu. .abu ry. dy. "sy. .ubu py. .ebu ry. py. .ibu gy." fy. .ibu ny. .ebu
OpenGL is a language

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

Advertisement
That macro would probably destroy Visual Studio''s autocomplete. You''d get your class and then DEFINE_VARIABLE in the menu. Yeah, exactly what you wanted!

At least that has been my experience with macros in class headers.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
I hate macros that change the accessor rules for a given class, they lead to hard-to-spot errors. I also strongly dislikes get/set-methods that provides no check on the data, then I rather keep my data public.
Sorry, but I just don''t think this macro is usable at all.
Macros are not your best friend, see them more as the kind of (clumsy?) friend who once or twice gets you out of a tight spot, but most of the time really makes your life a hell (because they screw things up).

This topic is closed to new replies.

Advertisement