By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,195 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

passing const char* to the string&

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.