Jump to content
  • Advertisement
Sign in to follow this  
eGamer

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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++)
what about this fact in cbuilder ?

Share this post


Link to post
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 this post


Link to post
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.
*/

}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!