[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-devel
Subject:    Re: strings and QStrings
From:       Bo Thorsen <bo () sonofthor ! dk>
Date:       2000-12-10 9:17:43
[Download RAW message or body]

On Sun, 10 Dec 2000, David van Hoose wrote:
> SNIP
> > 
> > This prints out "constructor" twice. It has no effects to change
> > optimization levels or not optimize or debug or whatever. So, I repeat
> > what I said: "Class c = object" will always call the copy constructor and
> > not first call constructor and then operator=.
> 
> I know what it prints. A function that returns the object calls the copy
> constructor.

Please read the code again, no function is returning an object and thereby
calling the copy constructor. The constructors are called only in the
creation of the two objects of type B.

The discussion was sparked by someone claiming that it's better to do
"Object o(someOtherObject)" than "Object o = someOtherObject" which is
simply not true since they generate the same code. If you keep saying that
it generates different code, I suggest you look up the part in the ISO
specs where this specific optimization is mentioned. I'm not going to
waste my time doing so.

Bo.

Since you cut out the code being discussed, here it is again:

#include <iostream>

class A {
public:
    int i;
};

class B {
public:
    B(){}
    B(const A &a) { i = a.i; std::cout << "constructor\n"; }
    B &operator=(const A &a)
        { i = a.i; std::cout << "operator=\n"; return *this; }
private:
    int i;
};

int main(int argc, char **argv) {
    A a;
    B b1 = a;
    B b2(a);
    return 0;
}

-- 

Bo Thorsen
bo@sonofthor.dk
Lahnsgade 31, st.
DK-5000 Odense C
Tlf: +45 66 11 83 85

Rule one of good programmers: anything You can do, I can do better
Rule two of good programmers: anything I can do, You can do better
The Programming Theorem: anything worth doing is worth doing over and over again

 
>> Visit http://master.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic