473,246 Members | 1,594 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,246 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 9359

<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: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.