string literal as const string& parameter

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • selder21@hotmail.com

    string literal as const string& parameter

    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.
  • Peter van Merkerk

    #2
    Re: string literal as const string& parameter


    <selder21@hotma il.com> wrote in message
    news:ebf780cd.0 309170129.a13bb 8a@posting.goog le.com...[color=blue]
    > 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.[/color]

    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.merke rk(at)dse.nl




    Comment

    • Ivan Vecerina

      #3
      Re: string literal as const string&amp; parameter

      <selder21@hotma il.com> wrote in message
      news:ebf780cd.0 309170129.a13bb 8a@posting.goog le.com...
      | 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
      --
      Ivan Vecerina - expert in medical devices, software - info, links, contact information, code snippets



      Comment

      • Ron Natalie

        #4
        Re: string literal as const string&amp; parameter


        <selder21@hotma il.com> wrote in message news:ebf780cd.0 309170129.a13bb 8a@posting.goog le.com...[color=blue]
        > Hello,[/color]
        [color=blue]
        >
        > Ex::Ex(const string& param){ ... }
        > Ex::Ex(const char* param){ string temp = string(param); Ex(temp);}
        >[/color]
        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.


        Comment

        • Mike Wahler

          #5
          Re: string literal as const string&amp; parameter


          <selder21@hotma il.com> wrote in message
          news:ebf780cd.0 309170129.a13bb 8a@posting.goog le.com...[color=blue]
          > 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.[/color]

          What 'conflicts'? Are you getting a particular compiler
          error message?
          [color=blue]
          > 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.[/color]

          Yup. :-)

          #include <iostream>
          #include <string>

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

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

          -Mike



          Comment

          • Frank Schmitt

            #6
            Re: string literal as const string&amp; parameter

            selder21@hotmai l.com (selder21@hotma il.com) writes:
            [color=blue]
            > 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.[/color]

            ?? 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

            Comment

            Working...