473,395 Members | 2,253 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 9369

<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Steven T. Hatton | last post by:
In the following code, the only way I can figure out to pass an array of const is by setting the template argument to const in the instanciation expression. It would be (or seem to me) better if I...
7
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
4
by: songkv | last post by:
Hi, I am trying to reassign an array of char to a string literal by calling a function. In the function I use pointer-to-pointer since I want to reassign the "string array pointer" to the string...
4
by: Locusta | last post by:
Hello, I have been struggeling for replacing a string in a string. The snippet from the program below replaces the <, & and > with the XML equivalent values. In the program, I allocate space...
12
by: weaselboy1976 | last post by:
Hello, If we have c code like what's below, we will get an error because in the stringManipulator function we attempt to modify a string literal on the second call to the function. My question...
10
by: lchian | last post by:
Hi, For two stl strings s1 and s2, I got different results from strcmp(s1.c_str(), s2.c_str()) and s1.compare(s2) can someone explain what these functions do? It seems that strcmp gives...
41
by: Dead Loop | last post by:
Hi all, I'm a beginner and my question is: Are there any differences between char *p = "Hello, world!"; and const char *p = "Hello, world!"; ?
3
by: yogi_bear_79 | last post by:
I'm sure I have a few things wrong here. But I am stuck on how to do a recurring search. Also my statement cin >quote; acts weird. If I enter more than one word it blows right past cin >findMe;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.