Using define to shorten things up

Started by
13 comments, last by MaulingMonkey 16 years, 11 months ago
Quote:Original post by raptorstrike
I was wondering if this was a good idea, it works and seems flexible but could possibly lead to confusion and might just be bad form which is what Im here, to find out...


Well, I think that what you are trying to do is a good idea, but the way you propose accomplishing it is not very practical. I think the idea of naming the contructors and destructor functions using the name of the class has problems. I would have preferred the names of the constructors and destructor be "constructor" and "destructor".

Along the same lines, this kind of thing is common:
    class foo : public bar    {        typedef bar BaseClass;        ...    }; 

John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Advertisement
I had a similar(somewhat more complex) idea for using defines to implement a switch for types other than integrals. While it worked, I decided not to use it because actually changing the syntax of the language is not a good thing. For better or for worse, C++ has constructors and destructors named after the class and not aliased with names like "CLASS","~CLASS","__init__" ,"__del__" or what have you. And if you're using C++, you must write in C++, otherwise you're just causing confusion. Using #define like that is nasty, because you're not using an "proper" language mechanism, just brute text substitution.

If you're seeking for ideas like this in the future, I suggest you look into templates and compile-time metaprogramming, they can "transform",so to speak, the language as well, but this time by playing by the rules ;)
Quote:Original post by JohnBolton
Quote:Original post by raptorstrike
I was wondering if this was a good idea, it works and seems flexible but could possibly lead to confusion and might just be bad form which is what Im here, to find out...


Well, I think that what you are trying to do is a good idea, but the way you propose accomplishing it is not very practical. I think the idea of naming the contructors and destructor functions using the name of the class has problems. I would have preferred the names of the constructors and destructor be "constructor" and "destructor".


Or you could go with D's style and use this() and ~this...

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Like most others have said I think its a very bad idea, but, there are also times when very similar things are justifiable.

Example:
/*************************************************************/#define BINARY_OPERATOR(name, op)                             \ template<typename LHS, typename RHS>                          \ struct name                                                   \ {                                                             \     name(LHS lhs, RHS rhs): lhs(lhs), rhs(rhs) { }            \     LHS lhs; RHS rhs;                                         \ };                                                            \                                                               \ template<typename LHS, typename RHS>                          \ name<LHS, RHS> op(LHS lhs, RHS rhs)                           \ { return name<LHS, RHS>(lhs, rhs); }                          \ /*************************************************************/BINARY_OPERATOR(add_op      , operator+ )BINARY_OPERATOR(sub_op      , operator- )BINARY_OPERATOR(mul_op      , operator* )BINARY_OPERATOR(div_op      , operator/ )BINARY_OPERATOR(add_equal_op, operator+=)BINARY_OPERATOR(sub_equal_op, operator-=)BINARY_OPERATOR(mul_equal_op, operator*=)BINARY_OPERATOR(div_equal_op, operator/+)#undef BINARY_OPERATOR


Edit: forum screwed up macro, had to add spaces after '\'s
Boo Aiming to save a few keystrokes is not a terribly admirable goal1. In the original method, given that you're also increasing the strain of identifying constructors and the like2 with macros, you have nothing but sheer madness. This is not Sparta. On the other hand, a class-private typedef can be a sane keystroke saver, although I only use such a thing for returning *this from operators in classes with long names.

[1] It's not necessairly a bad one either, but other considerations trump it in terms of importance.
[2] Because the C++ programmer is used to looking for his class name, not CLASS, as his constructor/destructor identifier.

This topic is closed to new replies.

Advertisement