Which constructor am I missing?

This is a discussion on Which constructor am I missing? within the C++ Programming forums, part of the General Programming Boards category; In this code: Code: class C { public: explicit C() {}; explicit C(const C& c) {}; ~C() {}; }; C ...

  1. #1
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Which constructor am I missing?

    In this code:
    Code:
    class C
    {
    	public:
    		explicit C() {};
    		explicit C(const C& c) {};
    		~C() {};
    };
    
    C Create()
    {
    	return C();
    }
    I get the error (pointing at the return-part in Create()):

    error C2520: 'C::C' : no non-explicit constructor available for implicit conversion
    What am I missing? Shouldn't the copy constructor be enough?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  2. #2
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    A constructor only needs to be qualified as explicit if it takes a single argument and you don't want implicit conversions to be made. You want implicit conversions with the copy constructor, or it'll be nearly useless. Just remove the explicit keyword from the default and copy constructors and it'll work fine.
    Just because I don't care doesn't mean I don't understand.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Perhaps I'm missing something, but what implicit conversion is made in the code above?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    but what implicit conversion is made in the code above?
    The one that I've colored red for you:
    Code:
    class C
    {
    	public:
    		explicit C() {};
    		explicit C(const C& c) {};
    		~C() {};
    };
    
    C Create()
    {
    	return C();
    }
    Just because I don't care doesn't mean I don't understand.

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Converting from C to C is implicit? Even though I explicitly give a constructor for converting from C to C? I feel so confuzzled...

    I thought impicit conversions was like having a class C with a member int i, then typing C c = 34; and the constructor automagically assigns the member variable to 34...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,339
    C() constructs a temporary instance of C with the default constructor. Where do you explicitly call the copy constructor? You don't, which is why it is implicit and you get the error. As Narf said you only explicit is only useful for single argument constructors (or constructors where all but one of the parameters have optional values).

  7. #7
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    Converting from C to C is implicit? Even though I explicitly give a constructor for converting from C to C? I feel so confuzzled...
    You call the default constructor explicitly, then pass it as a return value where the copy constructor is called implicitly behind the scenes. Just because a compiler might implement return value optimization doesn't mean you can suddenly break language rules. At least, I assume you're confused because of return value optimization... If not, forget I mentioned it because it just confuses the point.
    Just because I don't care doesn't mean I don't understand.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 02:49 PM
  3. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 03:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21