string literal as const string& parameter

Discussion in 'C++' started by selder21@hotmail.com, Sep 17, 2003.

  1. Guest

    Hello,

    I have a class with constructor taking a const string&. Now i want to
    call this constructor with a string literal. Because this is of type
    char* there are overload resolution conflicts.
    If i make another constructor with parameter const char*, how can i
    call the constructor with the const string& ?

    I tried

    Ex::Ex(const string& param){ ... }
    Ex::Ex(const char* param){ string temp = string(param); Ex(temp);}

    but this gives compile errors.

    Greetings, Tom.
     
    , Sep 17, 2003
    #1
    1. Advertising

  2. <> wrote in message
    news:...
    > Hello,
    >
    > I have a class with constructor taking a const string&. Now i want to
    > call this constructor with a string literal. Because this is of type
    > char* there are overload resolution conflicts.
    > If i make another constructor with parameter const char*, how can i
    > call the constructor with the const string& ?
    >
    > I tried
    >
    > Ex::Ex(const string& param){ ... }
    > Ex::Ex(const char* param){ string temp = string(param); Ex(temp);}
    >
    > but this gives compile errors.


    You cannot call a construtor from another constructor in the same class.
    Besides that the Ex::Ex(const char* param) constructor isn't needed
    because the compiler knows how to convert a const char* to a
    std::string:

    #include <string>
    using std::string;

    class Ex
    {
    public:
    Ex(const string& param) {};
    };

    int main()
    {
    Ex e("bla"); // A temporary std::string instance will be created here
    return 0;
    }

    --
    Peter van Merkerk
    peter.van.merkerk(at)dse.nl
     
    Peter van Merkerk, Sep 17, 2003
    #2
    1. Advertising

  3. <> wrote in message
    news:...
    | I have a class with constructor taking a const string&. Now i want to
    | call this constructor with a string literal. Because this is of type
    | char* there are overload resolution conflicts.
    | If i make another constructor with parameter const char*, how can i
    | call the constructor with the const string& ?
    |
    | I tried
    |
    | Ex::Ex(const string& param){ ... }
    | Ex::Ex(const char* param){ string temp = string(param); Ex(temp);}
    |
    | but this gives compile errors.
    Yes. Unfortunately, in the current C++ standard, it is not legally
    possible for a constructor to 'forward' the construction to one
    of its overloads.
    The only valid approach to share code among construcor is to
    put this code in a private init() member function
    (which makes it impossible to share member initializations).

    This said, what is the overload resolution conflict that you
    are encountering ?
    Because:
    Ex::Ex(const std::string& param){ ... }
    can be called with a string literal:
    Ex* p = new Ex("a literal");

    Worst case, explicit construction is always possible:
    Ex* p = new Ex(std::string("a literal"));


    hth,
    Ivan
    --
    http://ivan.vecerina.com
     
    Ivan Vecerina, Sep 17, 2003
    #3
  4. Ron Natalie Guest

    <> wrote in message news:...
    > Hello,


    >
    > Ex::Ex(const string& param){ ... }
    > Ex::Ex(const char* param){ string temp = string(param); Ex(temp);}
    >

    1. You can't call constructors.
    2. You don/t need the const char* constructor because string has
    an converting constructor for const char*. A temporary string object
    is made and bound to your param reference.
     
    Ron Natalie, Sep 17, 2003
    #4
  5. Mike Wahler Guest

    <> wrote in message
    news:...
    > Hello,
    >
    > I have a class with constructor taking a const string&. Now i want to
    > call this constructor with a string literal. Because this is of type
    > char* there are overload resolution conflicts.


    What 'conflicts'? Are you getting a particular compiler
    error message?

    > If i make another constructor with parameter const char*, how can i
    > call the constructor with the const string& ?
    >
    > I tried
    >
    > Ex::Ex(const string& param){ ... }
    > Ex::Ex(const char* param){ string temp = string(param); Ex(temp);}
    >
    > but this gives compile errors.


    Yup. :)

    #include <iostream>
    #include <string>

    void foo(const std::string& parm)
    {
    std::cout << parm << '\n';
    }

    int main()
    {
    foo("Hello world");
    return 0;
    }

    -Mike
     
    Mike Wahler, Sep 17, 2003
    #5
  6. () writes:

    > Hello,
    >
    > I have a class with constructor taking a const string&. Now i want to
    > call this constructor with a string literal. Because this is of type
    > char* there are overload resolution conflicts.


    ?? The following should work on any conforming C++ compiler:

    #include <iostream>
    #include <string>

    class Ex {
    public:
    Ex(const std::string& s) {
    std::cout << "Ex created with " << s << std::endl;
    }
    };

    int main() {
    std::string s("blubb");
    Ex e2(s);
    Ex e1("hello, world");
    return 0;
    }

    So your problem must lie elsewhere - show some code.

    kind regards
    frank

    --
    Frank Schmitt
    4SC AG phone: +49 89 700763-0
    e-mail: frankNO DOT SPAMschmitt AT 4sc DOT com
     
    Frank Schmitt, Sep 22, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    11
    Views:
    1,143
  2. Javier
    Replies:
    2
    Views:
    604
    James Kanze
    Sep 4, 2007
  3. 0m
    Replies:
    26
    Views:
    1,159
    Tim Rentsch
    Nov 10, 2008
  4. fungus
    Replies:
    13
    Views:
    929
    fungus
    Oct 31, 2008
  5. Anonieko Ramos

    What's wrong with rpc-literal? Why use doc-literal?

    Anonieko Ramos, Sep 27, 2004, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    400
    Anonieko Ramos
    Sep 27, 2004
Loading...

Share This Page