Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > string literal as const string& parameter

Reply
Thread Tools

string literal as const string& parameter

 
 
selder21@hotmail.com
Guest
Posts: n/a
 
      09-17-2003
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.
 
Reply With Quote
 
 
 
 
Peter van Merkerk
Guest
Posts: n/a
 
      09-17-2003

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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




 
Reply With Quote
 
 
 
 
Ivan Vecerina
Guest
Posts: n/a
 
      09-17-2003
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      09-17-2003

<(E-Mail Removed)> wrote in message news:(E-Mail Removed) 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.


 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      09-17-2003

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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



 
Reply With Quote
 
Frank Schmitt
Guest
Posts: n/a
 
      09-22-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) ((E-Mail Removed)) 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<const MyType> Vs const vector<MyType> magnus.moraberg@gmail.com C++ 2 02-09-2009 10:45 PM
is const necessary in eg int compar(const void *, const void *) lovecreatesbeauty@gmail.c0m C Programming 26 11-10-2008 09:47 PM
const correctness - should C++ prefer const member over non-const? fungus C++ 13 10-31-2008 05:33 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM



Advertisments