Constructor takes string => constructor not called...? (C++)
Hi there,
I have a File class and I want to call its constructor. It has three constructors, they look like this: Code:
File(int fd = -1); // Construct from file descriptor... Code:
// Using just "File file(argv[1])" seemed to want to use the When I change the call to: Code:
File file(std::string(argv[1]), File::RdOnly); Can anybody tell me what's going on here? |
Do you have a derived class with File as a base containing a constructor that takes a single std::string argument? You might try declaring all of your constructors (other than default and copy) explicit.
Kevin Barry |
Thanks for the suggestion - no, the only derived class is Socket and that has no such constructor. Also, File does not derive from any class. Still, declaring the instance like "File file(/* args */)" surely should never cause any other constructor to be called, derived or otherwise, should it?
|
Quote:
Code:
File file(std::string(argv[1])); I wish I could offer a better explanation on how to avoid errors like this; as you discovered, the compiler does not issue any warning/error. Try calling the function file() with an std::string parameter, and then you will get the error. Play with the code that is commented out: Code:
#include <string> |
Quote:
Code:
struct F Code:
test.cpp: In function 'int main()': |
All times are GMT -5. The time now is 12:43 AM. |