public inbox for gcc-prs@sourceware.org help / color / mirror / Atom feed
From: martin.gerbershagen@icn.siemens.de To: gcc-gnats@gcc.gnu.org Subject: c++/5995: double call of copy constructor Date: Mon, 18 Mar 2002 03:16:00 -0000 [thread overview] Message-ID: <20020318111112.14467.qmail@sources.redhat.com> (raw) >Number: 5995 >Category: c++ >Synopsis: double call of copy constructor >Confidential: no >Severity: serious >Priority: medium >Responsible: unassigned >State: open >Class: wrong-code >Submitter-Id: net >Arrival-Date: Mon Mar 18 03:16:01 PST 2002 >Closed-Date: >Last-Modified: >Originator: Martin Gerbershagen >Release: g++-3.0.3 >Organization: >Environment: sparc-sun-solaris2.6 >Description: The attached test program x.C generates the following output: O::O() o1(getOcopy()) O::O(const O& s) O::O(const O& s) O::~O() o2(getOref()) O::O(const O& s) &o3(getOref()) o4 = getOref() O::O(const O& s) &o5 = getOcopy() O::O(const O& s) o6 = getOcopy() O::O(const O& s) o7 = getOcopy() O::O() O::O(const O& s) O::operator= O::~O() o7 = getOref() O::operator= (void)getOcopy() O::O(const O& s) O::~O() end O::~O() O::~O() O::~O() O::~O() O::~O() O::~O() O::~O() The output shows, that the copy constructor of O is called twice during the initialization of o1. This should not be the case and can result in performance degradation, if the class used has complex members. The test program works properly, if it is compiled with g++ 2.95.3. >How-To-Repeat: g++ -O x.C && a.out >Fix: >Release-Note: >Audit-Trail: >Unformatted: ----gnatsweb-attachment---- Content-Type: text/plain; name="x.C" Content-Disposition: inline; filename="x.C" #include <iostream.h> class O { public: O& operator=(const O&); O(const O&); O(); ~O(); }; O& O::operator=(const O& s) { cout << "O::operator=" << endl; if (this != &s) { } return *this; } O::O(const O& s) { cout << "O::O(const O& s)" << endl; } O::O() { cout << "O::O()" << endl; } O::~O() { cout << "O::~O()" << endl; } class A { O o; public: O getOcopy() const; const O& getOref() const; A& operator=(const A&); A(const A&); A(); ~A(); }; inline O A::getOcopy() const { return o; } inline const O& A::getOref() const { return o; } A& A::operator=(const A& s) { if (this != &s) { o = s.o; } return *this; } A::A(const A& s): o(s.o) { } A::A() { } A::~A() { } int main() { A a; cout << "o1(getOcopy())" << endl; O o1(a.getOcopy()); cout << "o2(getOref())" << endl; O o2(a.getOref()); cout << "&o3(getOref())" << endl; const O &o3(a.getOref()); cout << "o4 = getOref()" << endl; O o4 = a.getOref(); cout << "&o5 = getOcopy()" << endl; const O &o5 = a.getOcopy(); cout << "o6 = getOcopy()" << endl; O o6 = a.getOcopy(); cout << "o7 = getOcopy()" << endl; O o7; o7 = a.getOcopy(); cout << "o7 = getOref()" << endl; o7 = a.getOref(); cout << "(void)getOcopy()" << endl; a.getOcopy(); cout << "end" << endl; return 0; }
next reply other threads:[~2002-03-18 11:16 UTC|newest] Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top 2002-03-18 3:16 martin.gerbershagen [this message] 2002-03-18 6:27 jason 2002-03-18 8:58 jason
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20020318111112.14467.qmail@sources.redhat.com \ --to=martin.gerbershagen@icn.siemens.de \ --cc=gcc-gnats@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).