This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
3
When and why should I use constructors with initialization lists?
Post Flair (click to view more posts with a particular flair)
Post Body
Here is the sample code:
class Human
{
private:
int age;
std::string name;
public:
Human(std::string humansName = "Adam", int humansAge = 25):name(humansName), age(humansAge) {
std::cout << "Constructed a human called " << name;
std::cout << ", " << age << " years old" << std::endl;
}
};
versus this:
class Human
{
private:
int age;
std::string name;
public:
Human(std::string humansName = "Adam", int humansAge = 25) {
age = humansAge;
name = humansName;
std::cout << "Constructed a human called " << name;
std::cout << ", " << age << " years old" << std::endl;
}
};
Why does the constructors with initialization lists exist? What are the use cases? Why should it be preferred? Thank you for your responses.
Author
User Disabled
Account Strength
0%
Disabled 1 month ago
Account Age
4 years
Verified Email
Yes
Verified Flair
No
Total Karma
187
Link Karma
103
Comment Karma
84
Profile updated: 7 hours ago
Posts updated: 1 year ago
Subreddit
Post Details
We try to extract some basic information from the post title. This is not
always successful or accurate, please use your best judgement and compare
these values to the post title and body for confirmation.
- Posted
- 3 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/cpp_questio...