Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: Inaccessible but unused copy constructor causes compile fail

Reply
Thread Tools

Re: Inaccessible but unused copy constructor causes compile fail

 
 
goran.pusic@gmail.com
Guest
Posts: n/a
 
      11-23-2012
On Wednesday, November 21, 2012 10:30:47 PM UTC+1, Andy Champ wrote:
> Compiling this...
>
>
>
> struct ncnd
>
> {
>
> ncnd()
>
> {
>
> }
>
> private:
>
> ncnd(const ncnd& other);
>
> };
>
>
>
> void main()
>
> {
>
> ncnd x = ncnd();
>
> }
>
>
>
>
>
> Comeau complains:
>
> line 12: error: "ncnd::ncnd(const ncnd &)" (declared at line 7),
>
> required for copy that was eliminated, is inaccessible
>
> ncnd x = ncnd();
>
>
>
> (it also doesn't like void main() but that's beside the point)
>
>
>
> MSVC (2010 and 2012) also fails - but when I comment out the private it
>
> compiles, links and runs just fine. Why does it need the copy
>
> constructor when it's about to eliminate it?


Because compiler wants to use copy ctor where it looks like you have assignment.

Compiler is treating

type t = type()

like

type t(type())

(Hence the error).

Think about the following: your code looks like it should do the default ctor + assignment. Why would you want this? It's longer to write and less efficient than copy construction, but amounts to same (your object ends up being a copy of another one).

So the standard sayt that the compiler should use copy ctor when it sees assignment like yours.

If you __must__ have default construction and assignment (hint: you don't, consider rule of three[1]), you can:

ncnd x;
x = ncnd();


HTH,

Goran.

[1] http://en.wikipedia.org/wiki/Rule_of_Three
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Inaccessible but unused copy constructor causes compile fail Ian Collins C++ 0 11-21-2012 09:34 PM
template copy constructor vs normal copy constructor cinsk C++ 35 10-10-2010 11:14 PM
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
Copy construction with inaccessible base class copy c-tor Victor Bazarov C++ 15 03-04-2007 03:47 PM
inaccessible constructor cpp_novice@yahoo.com C++ 7 08-04-2006 05:11 PM



Advertisments