From -5161179836695583 X-Google-Thread: f78e5,51456c3eb8f9490e X-Google-Attributes: gidf78e5,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news2.google.com!news.germany.com!multikabel.net!feed20.multikabel.net!news.tele.dk!news.tele.dk!small.news.tele.dk!lnewsinpeer00.lnd.ops.eu.uu.net!emea.uu.net!peer-uk.news.demon.net!kibo.news.demon.net!news.demon.co.uk!demon!stump.algebra.com!devnull From: v.Abazarov@comAcast.net ("Victor Bazarov") Newsgroups: comp.std.c++ Subject: Re: Copy Constructor Confusion! Date: Tue, 15 Aug 2006 16:03:10 GMT Organization: Datemas.de http://datemas.de Lines: 79 Sender: mail2news@demon.net Approved: fjh@cs.mu.oz.au (Fergus Henderson , moderator of comp.std.c++) Message-ID: References: <1155645873.296811.3890@74g2000cwt.googlegroups.com> NNTP-Posting-Host: news.news.demon.net X-Trace: news.demon.co.uk 1155657799 7504 158.152.254.254 (15 Aug 2006 16:03:19 GMT) X-Complaints-To: abuse@demon.net NNTP-Posting-Date: Tue, 15 Aug 2006 16:03:19 +0000 (UTC) X-Original-To: std-c++@mailman.ucar.edu X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov) X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2900.2962 X-Priority: 3 X-RFC2646: Format=Flowed; Original X-Virus-Scanned: amavisd-new at csse.unimelb.edu.au X-MSMail-Priority: Normal X-Received: (from fjh@localhost) by mulga.csse.unimelb.edu.au (8.13.6+Sun/8.13.6/Submit) id k7FG3ABp003119; Wed, 16 Aug 2006 02:03:10 +1000 (EST) X-Path: comp-std-cpp-robomod!not-for-mail X-NNTP-Posting-Date: Tue, 15 Aug 2006 15:22:14 +0000 (UTC) X-Delivered-To: std-c++@mailman.ucar.edu X-Authentication-Warning: mulga.csse.unimelb.edu.au: fjh set sender to devnull@stump.algebra.com using -f X-Newsreader: Microsoft Outlook Express 6.00.2900.2869 X-Newsgroups: comp.std.c++ Xref: g2news2.google.com comp.std.c++:3269 Shouldn't this be moved to a .lang. newsgroup? Just checking... Frederick Gotham wrote: > Thomas posted: > >> Hello fellow programmers! >> >> I have a c++ line: >> >> X x = X(); >> >> Does the standard say that the class X must have a public copy >> constructor for this to be legal, even if the copy constructor never >> gets called? > > > Yes, although it gives explicit permission to the compiler to elide > the creation of the redudant temporary object. Even if the temporary > is elided, the copy-constructor must still be public. Not "public", *accessible*. If the "c++ line" above appears inside a friend function, it would work just fine with a private copy c-tor. If it appears inside a member of a derived class, "protected" should suffice. >> So is this legal for the optimizer, or whatever, to optimize this >> copy constructor away, and just create code like I've writen "X x;" ? > > > Yes. Even if no temporary is created, the following two definitions > are *not* equivalent if you're working with a type which hasn't got a > constructor: > > X x = X(); /* Get's default-initialised */ > > X x; /* Doesn't get initialised unless there's a constructor */ That depends on whether 'X' is a POD or not. If it's not a POD but it doesn't have a [user-defined] constructor, but all it members have them, it is going to be default-initialised just fine. >> If I am right, can anyone explain why it is so? > > > There are better ways to achieve what you want. Here's my own > particular favourite way of default-initialising an object: > > template > void Func() > { > T arr[1] = {}; > > T &obj = *arr; > > /* Now work with "obj" */ > } To OP: you don't have to use templates to do what you want. Apply the same technique and just replace 'T' with 'X'. BTW, you can write this in the same line, AFAIK: X arr_[2] = {}, &x = arr_[0], &xx = arr_[1]; // two objects and by utilizing some preprocessor magic, you can make up unique names for the arrays (if there are several such lines in your scope). I'll leave that exercise to the reader. V -- Please remove capital 'A's when replying by e-mail I do not respond to top-posted replies, please don't ask --- [ comp.std.c++ is moderated. To submit articles, try just posting with ] [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ] [ --- Please see the FAQ before posting. --- ] [ FAQ: http://www.comeaucomputing.com/csc/faq.html ]