Implicit class-type conversions

This is a discussion on Implicit class-type conversions within the C++ Programming forums, part of the General Programming Boards category; Ok... As I'm studying classes I'm confronted with this feature: A constructor that can be called with a single argument ...

  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,535

    Implicit class-type conversions

    Ok... As I'm studying classes I'm confronted with this feature:

    A constructor that can be called with a single argument defines an implicit conversion from the parameter type to the class type.
    While I understand the concept, I have trouble understanding something else... Given the following 3 constructors and member function,

    Code:
    MyClass(const string &str = ""): name(str), value(0) { }
    MyClass(istream &is): name(is), value(0) {}
    MyClass(const string &str, int val): name(str), value(val) { }
    
    int MyClass::do_something(const myClass &obj) const;
    I know that only the two first constructors define an implict conversion.
    So, if I pass a string to do_something(), the first constructor is used to convert it to a temporary MyClass. Whereas if I pass an istream object, it is the 2nd constructor the one being used to make the implicit conversion.

    But why is that I'm being told the istream option is poor class design? Isn't it conceivable that I may want such behavior?
    Last edited by Mario F.; 06-16-2006 at 05:38 PM.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Making things implicit is often poor design because it can surprise the users of your class. If they want to pass a MyClass instance initialized with an istream& to do_something, they can pass MyClass(my_is) explicitly.

    BTW, that example doesn't look right. If it is a full class, perhaps the poor design is also related to your name member.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,535
    Yes, the class was just an example.

    Ok, understood on that. So, is it common for constructors to be declared explicit? Unless I have a strong reason not to, should I do it to avoid the implicit conversion?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Yes, you should declare single parameter constructors as explicit unless you specifically want the implicit conversion and have a good reason to do so.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,535
    Ok, Daved. Thanks for that.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 04:44 AM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Replies: 8
    Last Post: 10-02-2005, 01:27 AM
  4. Erros in Utility Header File
    By silk.odyssey in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2003, 06:17 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

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