Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C++ question

Name: Anonymous 2013-08-28 13:28

Do I use

MyClass::MyClass (const char* description)
{
    this->description = description;
}
MyClass::MyClass (const std::string& description)
{
    this->description = description;
}


or

MyClass::MyClass (const std::string description)
{
    this->description = description;
}


?

Name: Anonymous 2013-08-28 13:44

The correct answer is

MyClass::MyClass (const std::string& description)
{
    this->description = description;
}

Unless you're interfacing with C or legacy code that needs char*. All classes/structs in C++ are value types, so you should never do

MyClass::MyClass (const std::string description)
{
    this->description = description;
}

which would cause the string to be copied.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List