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

List:       kde-devel
Subject:    Re: Philosophical question
From:       "Joshua J. Berry" <des () condordes ! net>
Date:       2006-11-17 16:22:20
Message-ID: 200611170822.25794.des () condordes ! net
[Download RAW message or body]

[Attachment #2 (multipart/signed)]


On Friday 17 November 2006 07:37, Frerich Raabe wrote:
> On Friday 17 November 2006 16:20, Thiago Macieira wrote:
> > Remi Villatel wrote:
> > >Now, what are the differences in between
> > >
> > >QSomeThing anotherOne = thatThing;
> > >
> > >and
> > >
> > >QSomeThing anotherOne(thatThing);
> > >
> > >?
> >
> > In most compilers, none.
>
> This is not correct. Using the '=' syntax will not work when the
> constructor which is invoked is explicit.

Huh?  Yes it is.  You can use the following program to convince yourself of 
its correctness.  You may note (on gcc 4.1, at least) that A's assignment 
operator is never called.  You may also note that the default constructor 
is never called when doing an initial assignment.

#include <iostream>

class A {
public:
    A() {
        std::cout << "A Default constructor\n";
    }

    A(const A & a) {
        std::cout << "A Copy constructor\n";
    }

    A & operator=(const A & a) {
        std::cout << "A Assignment operator\n";
        return *this;
    }
};

class B {
public:
    B() {
        std::cout << "B Default constructor\n";
    }

    B(const B & a) {
        std::cout << "B Copy constructor\n";
    }
};

int main(int argc, char ** argv) {
    A first;
    A second = first;
    A third(first);

    B b1;
    B b2 = b1;
    B b3(b1);

    return 0;
}

On my system, the program prints:

A Default constructor
A Copy constructor
A Copy constructor
B Default constructor
B Copy constructor
B Copy constructor

Which is what you would expect if "A second(first)" and "A second = first" 
are equivalent.

-- Josh


-- 
Joshua J. Berry

"I haven't lost my mind -- it's backed up on tape somewhere."
    -- /usr/games/fortune

[Attachment #5 (application/pgp-signature)]

>> Visit http://mail.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