Skip to main content
GameDev.net gamedev.net
🔒 Locked

C++ - Multiple names for one function?

Started by PolyVox May 18, 2006 at 5:51 AM 10 replies 2.8k views
Original Post
PolyVox
PolyVox
Hi all, I'm asking this out of interest rather then because it's a feature I absolutly need, but whats the best way to give a function multiple names in C++? Let's say I have a vector class with three functions - getX(), getY(), and getZ(). Now many people use a different convention and like to call the vector component i,j, and k so i'd also like to supply getI(), getJ(), and getK() functions which do exactly the same as thier x,y,z counterparts. Whats the best way to achieve this? I see several options: 1) Copy and paste the code and rename functions. 2) Have getI() call getX() internally. 3) Clever tricks with functions pointers/references?! 4) Clever tricks with templates? Although not exactly critical, I do believe this would be a useful language feature. Maybe some languages provide it already? Any thoughts? David
hh10k
hh10k
With a function like getI(), you're not going to save much code by calling getX() instead of copy-pasting return x. For functions that do have some code in them, calling getX() is the easiest way to go (and inlining should remove its cost).

However, I think adding tons of identical functions for accessing xyz/ijk/rgb just pollutes the class and makes code harder to understand. I'd just stick with xyz. If you don't like using xyz for something like rgb color values, you should create a class for that purpose.
Enigma
Enigma
Quote:
Effective C++ 2nd Edition by Scott Meyers
A large interface can also lead to confusion. Suppose you create a class that supports cognition for an artificial intelligence application. One of your member functions is called think, but you later discover that some people want the function to be called ponder, and others prefer the name ruminate. In an effort to be accommodating, you offer all three functions, even though they do the same thing. Consider then the plight of a potential client of your class who is trying to figure things out. The client is faced with three different functions, all of which are supposed to do the same thing. Can that really be true? Isn't there some subtle difference between the three, possibly in efficiency or generality or reliability? If not, why are there three different functions? Rather than appreciating your flexibility, such a potential client is likely to wonder what on earth you were thinking (or pondering, or ruminating over).

Σnigma
Katachi
Katachi
Quote:
Original post by Enigma
Quote:
Effective C++ 2nd Edition by Scott Meyers
A large interface can also lead to confusion. Suppose you create a class that supports cognition for an artificial intelligence application. One of your member functions is called think, but you later discover that some people want the function to be called ponder, and others prefer the name ruminate. In an effort to be accommodating, you offer all three functions, even though they do the same thing. Consider then the plight of a potential client of your class who is trying to figure things out. The client is faced with three different functions, all of which are supposed to do the same thing. Can that really be true? Isn't there some subtle difference between the three, possibly in efficiency or generality or reliability? If not, why are there three different functions? Rather than appreciating your flexibility, such a potential client is likely to wonder what on earth you were thinking (or pondering, or ruminating over).

Σnigma


hihi, I was just about to post almost the exact same lines from Meyers. :D
Aiursrage2k
Aiursrage2k
Use a define.
#define getI getX
Insufficent Information: we need more infromationhttp://staff.samods.org/aiursrage2k/
Zahlman
Zahlman
Personally I think get/set pairs are an abomination anyway, but I will include support for them...

// There are spaces after the backslashes in the macro;// you should take those out when you use it in code. They are only there// to accomodate the forum's parser#define MEMBER(x) private: \ int x; \ public: \ int get##x { return x; } \ void set##x(int x##_) { x = x##_; }#define VECTOR(name, x, y, z) struct name { \ MEMBER(x) \ MEMBER(y) MEMBER(z) }VECTOR(colour, r, g, b);colour c;


EDIT: P.S. I also agree with Enigma; I'm just bored. :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.