0

I want to make a function, and in different contexts it is better called by different names.

class box(){
    private:
    float posX;
    float size = 10;
    public:
    float speedX;
    float left(){ return posX; } //Any way to combine these?
    float posX(){ return posX; } //Any way to combine these?
    float right(){ return posX + size; }
};

box a;
box b;

bool checkCollide(){
    if(a.right() < b.left()){ return 0; } //Not colliding
    if(b.right() < a.left()){ return 0; } //Not colliding
    return 1; //Colliding
} //Comparing right and left makes more sense than anything else

void physics(){
    a.posX() += a.speedX;
    b.posX() += b.speedX;
    //Adding speed to position makes more sense than
    //adding speed to "left"
}
//Loop physics X times per second, and do something if there's a collision

or, is there a better way to do this? Can I make the left/right member automatically update any time the position or size changes, instead of recalculating for every call?

3
  • Either stick with different names, or make single method to return your value. Having different names for the same purpose can be confusing, though.
    – bash.d
    Mar 2, 2013 at 5:58
  • You could assign to function pointers...but may be a performance cost. I would just use a more generic label. Mar 2, 2013 at 6:00
  • Maybe I should just make posX public and do a.posX += a.speedX? Left/right comparisons would use .left() and .right() for more clear syntax. Edit: removing the function posX() as well.
    – AWTom
    Mar 2, 2013 at 18:14

3 Answers 3

3

If you are really obliged to do this, then just make the one function call the other:

// the function that does the hard job
float foo(float a, float b)
{
    // some heavy and complicated code
    // ...
    // some more black magic, etc.
    // finally:
    return sqrt(a * a + b * b);
}

// the function that pretends to do the hard job
float bar(float a, float b)
{
    return foo(a, b);
}

But you better not do this, it's quite bad style. Different names => different tasks. Same task => same name. Don't hurt the intuition of your fellows... ;-)

6
  • I suppose you may be correct but not in this instance. Project a simple and easy to use interface to those use it. Why have two names to do the same job unless you are a recruitment agent!!!
    – Ed Heal
    Mar 2, 2013 at 6:02
  • @EdHeal I suggest you start reading the answer backwards, then there'll be a chance you notice the last few sentences with my adivce.
    – user529758
    Mar 2, 2013 at 6:03
  • Sorry - You are right. Sleep is required along with bf+dog. Don't say that the mutt has lost the bf again!
    – Ed Heal
    Mar 2, 2013 at 6:14
  • @EdHeal Oh, I see :) No worries, happened to me.
    – user529758
    Mar 2, 2013 at 6:15
  • Your dog lost your boyfriend? Dog has turned up - got a call from the police station from boyfriend in a panic about my Mutt. He has been eating a bone for the last hour on the sofa. Anyway all at home.
    – Ed Heal
    Mar 2, 2013 at 7:37
0

Yes - Not write two functions that at the start do the same thing. I just hope that they do not diverge. Then you have problems!

0

If you're on C++11, or when using Boost, you can bind the left() function to an std::function variable. With C++11:

class box {
// ...
public:
    // ...
    float left() { return posX; }
    const std::function<float()> posx = std::bind(&box::left, this);

The const is needed, otherwise posx could be changed at runtime to point to a different function.

If you're not using a C++11 compiler but use Boost instead, then it's not that expressive, since you have to initialize posx in the ctor:

class box {
// ...
public:
    box() : posx = boost::bind(&box::left, this);
    // ...
    float left() { return posX; }
    const boost::function<float()> posx;

In both cases, you can now do:

box b;
b.left();
b.posx();

This method doesn't really have any advantages I can think of compared to having a posx() function and calling left() in it. But it's possible and so deserves a mention.

But I agree with what H2CO3 said: don't have two names for the same function. It's confusing.

6
  • posX has to be changed during runtime.
    – AWTom
    Mar 2, 2013 at 18:04
  • @user2125707 posX it different from posx. Your original code is all messed up in this regard because you used the same identifier for the private variable and the public function. In the example I provided, I changed the function posX to posx. I thought this would be self-explanatory.
    – Nikos C.
    Mar 2, 2013 at 18:08
  • Aren't posX and posX() syntactically different enough? Anyway, posX is private.
    – AWTom
    Mar 2, 2013 at 18:11
  • @user2125707 They do have different access, but there's no way to differentiate between them. When you write box::posX, the compiler does not know which one you mean. For this reason, many programmers add a suffix or prefix to private members. For example, for the private member you would use mPosX (the "m" stands for "member") or fPosX ("f" for "field") or posX_.
    – Nikos C.
    Mar 2, 2013 at 18:17
  • I should just get rid of the posX() function, make posX public, and refer directly to it when I'm not comparing left and right, like in the physics() function.
    – AWTom
    Mar 2, 2013 at 18:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.