string literal as const string& parameter

S

selder21

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.
 
P

Peter van Merkerk

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;
}
 
I

Ivan Vecerina

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

Ron Natalie

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.
 
M

Mike Wahler

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
 
F

Frank Schmitt

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top