FelineHazard 0 Newbie Poster

Hi all,
I am doing some c++ exercise about operator replacing. I have successfully replaced the + (plus) operator to work on a user-defined class. I understand these operators are order-sensitive: need to replace the operator for (class+int) and for (int+class). Thus, creating two replacements that do exactly the same.
My question is: can I have replacement2 execute replacement1 and return the result? This would have the advantage of 1) shorter code 2) no unnecessary repeats in code. If I need to change the way I do the replacements I'll need to worry about it only once.
Here's a sample code. I made up a class for circle. It has center of circle (x,y) and radius (r).
Now I want to define circle+int and int+circle to increase the radius of circle.

class circle {
private:
    float x,y; // circle center
    float r; //radius
public:
    circle(){}; // constructors
    circle(float X, float Y, float R) {
        x=X; y=Y; r=R;
    }
    // replacement operator +
    friend circle operator+(circle c, int num);
    friend circle operator+(int num, circle c);
};

// operator replacement1
circle operator+(circle c, int num) {
    circle tmp=c;
    tmp.r+=num;
    return(tmp);
}

// operator replacement2
circle operator+(int num, circle c) {
    circle tmp=c;
    tmp.r+=num;
    return(tmp);
}

Now, I'd like to have replacement2 call to replacement1, instead of writing the same code twice. Is it possible?

Edit: Exactly 2 seconds after hitting submit, it hit me, and I feel rather silly. But for progeny (and since I have already written such a lengthy post :D ) here's the solution:
Instead of writing 2 exacly identical replacements, we can just modify replacement2 to be:

// operator replacement2
circle operator+(int num, circle c) {
    circle tmp=c+num;
    return(tmp);
}

Since replacement1 is already well defined, replacement2 just uses replacement1.

-FH

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.