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.

Name: Anonymous 2013-08-28 13:57


MyClass::MyClass (const std::string &description_) :
    description(description_)
{
}

Name: Anonymous 2013-08-28 14:02

The correct answer is:

Lisp.

Name: Anonymous 2013-08-28 15:56

>>1,2
Well, refs are nice but remember that once that fucking thing is ref'd in your new class you have to keep track of the lifecycle of aaaalllll the motherfucking instances that use it.  So there's something to be said for copying - if you're using the same string for a bunch of instances, reference, and if not, probably copy.

Name: Anonymous 2013-08-28 16:40

>>4
[Thread over.

Name: Anonymous 2013-08-28 22:20

I think this article somewhat missed the boat by not mentioning the D programming language. Learning C first does not make other languages easier to learn. Yes everyone currently uses C and C++ for their low level code. But they are obsolete and frankly masochistic languages with error prone syntax. D will eventually get recognized for it's efficiency and power, and will displace them. So many people just parrot the same spiel about how students should learn languages that are currently hot, but are really old, outdated, and extremely poorly suited to handle tangible problems like concurrency. As long as everyone keeps learning and using antiquated languages everyone will suffer with substandard software and code bases that could be a lot cleaner and easier to maintain. Students will learn bad habits, and users have to cope with more bugs then they have to. Coffee and headaches for everyone. Progress isn't about what everyone has always done, it's about what they're going to do tomorrow. And what programming language you learn or use shouldn't be based on sheer popularity, but based on what is the best tool for the job. And in our now concurrent environment, many languages are dinosaurs.

Name: Anonymous 2013-08-29 0:06

>>3
Superior code is below: my_class::my_class(std::string const& description)
    : description_(description) {}

Name: Anonymous 2013-08-29 0:06

>>8
my_class::my_class(std::string const& description)
    : description_(description) {}

Name: Anonymous 2013-08-29 5:13

pls dont hack me D:

Name: Anonymous 2013-08-29 5:13

check em

Name: Anonymous 2013-09-07 9:30

What are you guys still in 90's?
The real correct answer is:

MyClass::MyClass (std::string description)
    : description(std::move(description)) {}

It works optimally whether passed an l-value or r-value and works for const char * as well.

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