# CBuilder code is different...

This topic is 4845 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

class A { public: int x; // constructors.... A(int arg):x(arg) {} // . // . // . // operators A operator + (A& a) { return A(x+a.x); } void operator = (A& a) { x = a.x;} }; in some place else when i write: A a(1),b,c(2); b = a + c; it displays a compile time error in CBuilder "operator + not implemented in type 'A' for arguments of same type" but nothing in vc6 how could i solve the problem for i am moving completely to CBuilder please help ? does standard c++ code differ from cbuilder code ?... how do you advice to overload the + operator ?...

##### Share on other sites
Try making your code const correct. That is change the arguments of your functions to const A & instead of just A &. What probably is happening is that CBuilder is actually complaining about binding an rvalue to non-const reference in a funny manner.

##### Share on other sites
however,try overloading the + operator without & and you will see that copy
constructor is not called ????

by value argument ---> copy constructor ( standard c++)

##### Share on other sites
well i have change the arguments to const and it worked well...

when i wrote :
A a,b(1),c(2);
a = b + A(2);

the compiler didn't accept it until i put const A& , however withoug that it was calling the operator + of A :
(void operator +(A other);)
and when it was called no copy constructor for other wass called why?

##### Share on other sites
Quote:
 however withoug that it was calling the operator + of A : (void operator +(A other);)

Why do you have such an operator?

Quote:
 and when it was called no copy constructor for other wass called why?

Not sure where you were expecting one, but most likely optimizations like return-value optimization (RVO) have eliminated the need for a copy-construction there. Code examples (see comments):

foo function() {
foo bar;
...
return bar;
}

The compiler can implement this one of two ways:
1) The generalized way, the return value is copy-constructed from bar.
2) The optimized way - if the compiler can deduce bar must be the return type, it "becomes" the return value.

Since the return value must be at a certain address, when you don't know which foo is to be used, it must be copy-constructed to that address when returning:

foo function() {    foo bar, baz;    if ( rand() % 1 ) return bar;    else return baz;    /* is bar or baz put at the return address? Neither, a new foo must be     * (copy) constructed at that address from whichever one is returned     */}foo function() {    foo the_only_foo;    return the_only_foo;    /* this function has only one path of logic, it allways returns     * the_only_foo. The compiler can be smart and simply place this foo at the     * return address, so it dosn't need to be copy-constructed later. This is     * an example of RVO.     */}foo function() {    if ( rand() % 1 ) {        foo a( 4 );        return a;    } else {        foo b( 3 );        return b;    }    /* No copy constrution here either, because we don't even start to construct     * foo until we can be certain which one we're going to return. When either     * a or b is constructed, they can be placed directly at the return address     * since it's known for certain which one will be used. Again, this is an     * example of RVO.     */}

1. 1
Rutin
33
2. 2
3. 3
4. 4
5. 5

• 13
• 9
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
633331
• Total Posts
3011390
• ### Who's Online (See full list)

There are no registered users currently online

×