471,585 Members | 1,594 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,585 software developers and data experts.

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.
Jul 19 '05 #1
5 9250

<se******@hotmail.com> wrote in message
news:eb*************************@posting.google.co m...
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


Jul 19 '05 #2
<se******@hotmail.com> wrote in message
news:eb*************************@posting.google.co m...
| 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
Jul 19 '05 #3

<se******@hotmail.com> wrote in message news:eb*************************@posting.google.co m...
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.
Jul 19 '05 #4

<se******@hotmail.com> wrote in message
news:eb*************************@posting.google.co m...
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

Jul 19 '05 #5
se******@hotmail.com (se******@hotmail.com) 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
Jul 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by Steven T. Hatton | last post: by
7 posts views Thread by al | last post: by
4 posts views Thread by songkv | last post: by
4 posts views Thread by Locusta | last post: by
12 posts views Thread by weaselboy1976 | last post: by
10 posts views Thread by lchian | last post: by
41 posts views Thread by Dead Loop | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.