473,388 Members | 1,230 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,388 software developers and data experts.

passing const char* to the string&

Hi,

If I have a method that has string reference as a parameter, what
happens if I pass a const char* variable to this method?

One thought is that a temporary string will be created in the stack
and the parameter will refer to this object. Is this correct?

Does this mean if a constructor of a class has a string reference
parameter, the temporary string that is created in the stack is
destroyed after the contruction of the object is complete?

** Example**
e.g:
class x {
public:
x(std::string& name);
};

void main()
{
const char* const text = "Name";
x newObject(name);
}

Can anyone help me understand what happens during and after the
newObject is created?

Thanks,
-PJ.

Mar 20 '07 #1
2 3413
ragged_hippy wrote:
Hi,

If I have a method that has string reference as a parameter, what
happens if I pass a const char* variable to this method?

One thought is that a temporary string will be created in the stack
and the parameter will refer to this object. Is this correct?
Yes, a temporary std::string is created.
>
Does this mean if a constructor of a class has a string reference
parameter, the temporary string that is created in the stack is
destroyed after the contruction of the object is complete?

** Example**
e.g:
class x {
public:
x(std::string& name);
Must be a "const std::string&"

x( const std::string & name );
};

void main()
{
const char* const text = "Name";
x newObject(name);
}

Can anyone help me understand what happens during and after the
newObject is created?
It would do somthing similar to this:-

void main()
{
const char* const text = "Name";
{
const std::string temp( text );
x newObject( temp );
// temp destructs just before leaving
}

}
Mar 20 '07 #2
ragged_hippy wrote:
Hi,

If I have a method that has string reference as a parameter, what
happens if I pass a const char* variable to this method?
Depends.
One thought is that a temporary string will be created in the stack
and the parameter will refer to this object. Is this correct?
If the reference refers to a const std::string, yes. Otherwise, you should
get a compile error, because binding a non-const reference to a temporary
is forbidden.
Does this mean if a constructor of a class has a string reference
parameter, the temporary string that is created in the stack is
destroyed after the contruction of the object is complete?
Yes.
** Example**
e.g:
class x {
public:
x(std::string& name);
Change that to:

x(const std::string& name);
};

void main()
Change that to:

int main()
{
const char* const text = "Name";
x newObject(name);
}

Can anyone help me understand what happens during and after the
newObject is created?
Well, before newObject is created, a temporary std::string is created and
filled with "Name". This string lives while the constructor of newObject is
running and is destroyed immediately afterwards.

Mar 20 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: selder21 | last post by:
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....
20
by: christopher diggins | last post by:
I have heard it is considered good practice to pass function parameters as const& as often as possible, is this true? Is it possible to go overboard? And if so why? Thanks a lot in advance...
25
by: Victor Bazarov | last post by:
In the project I'm maintaining I've seen two distinct techniques used for returning an object from a function. One is AType function(AType const& arg) { AType retval(arg); // or default...
6
by: p|OtrEk | last post by:
What is practic difference between this two declarations? If i want call my func with func("blah") i could write: 1) func(std::string const& arg1) 2) func(const std::string& arg1) Whats better to...
3
by: Marcin Kalicinski | last post by:
void f(const char *&text); Is this a const reference to char * or a reference to const char *? And how to write both of them? thank you, Marcin
1
by: Kip | last post by:
Greetings, Could anyone explain to me how the parameter type works here? I am also interested in the technical details of what is actually happening on the stack. I have never seen a "char *...
5
by: Jae | last post by:
Real(const string &fileName) { FILE * myInputFile = fopen(fileName, "rt"); ..... fclose(myInputFile);
2
by: pookiebearbottom | last post by:
Just looking for opinion on which of the 3 methods below people use in their code when they convert a 'const char *' to a 'const std::string &' came across #3 in someone's code and I had to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...

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.