Explicit constructor on strings

This is a discussion on Explicit constructor on strings within the C++ Programming forums, part of the General Programming Boards category; What is the return type of the string("some text") expression? Isn't it string? The following is telling me it is ...

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

    Explicit constructor on strings

    What is the return type of the string("some text") expression? Isn't it string?

    The following is telling me it is not. The explicit declaration specifier is blocking the initialization.

    Code:
    //with the following constructor...
    explicit someClass(const std::string&);
    
    //... this definition attempt fails
    someClass test = string("Rachmaninov");
    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 hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    The return type is a string but you don't understand how the explicit keyword works. This should work:
    Code:
    someClass test( string("Rachmaninov") );
    Quote Originally Posted by Nicolai M. Josuttis: The C++ Standard Library A Tutorial and Reference (Chapter 2)
    By using the keyword explicit, you can prohibit a single argument constructor from defining an automatic type conversion. A typical example of the need for this feature is in a collection class in which you can pass the initial size as constructor argument. For example, you could declare a constructor that has an argument for the inital size of a stack:
    Code:
    class Stack
    {
        explicit Stack(int size);
        ...
    };
    Here, the use of explicit is rather important. Without explicit this constructor would define an automatic type conversion from int to Stack. If this happens, you could assign an int to a Stack:
    Code:
    Stack s;
    
    ...
    
    s = 40;
    The automatic type conversion would convert the 40 to a stack with 40 elements and then assign it to s. This is probably not what was intended. By delaring the int constructor as explicit, such an assignment results in an error at compile time.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,436
    I'm not sure I was able to follow...

    Quote Originally Posted by Nicolai M. Josuttis: The C++ Standard Library A Tutorial and Reference (Chapter 2)
    By using the keyword explicit, you can prohibit a single argument constructor from defining an automatic type conversion.
    Being the return type for string() a string, how come there is an implicit conversion?

    Quote Originally Posted by idem
    The automatic type conversion would convert the 40 to a stack with 40 elements and then assign it to s. This is probably not what was intended.
    What was intended then?
    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
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Being the return type for string() a string, how come there is an implicit conversion?
    There's an implicit comversion from string to someClass. The "explicit" keyword requires you to call the constructor directly and not rely on someClass(string) to be called. Read about "explicit".
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,436
    Aha! Thanks both.

    So basically explicit removes all chances of a copy-initialization?
    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.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    It disallows implicit conversions that would require calling a constructor like aClass(int).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 02:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM

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