I have a class with a default constructor which initializes many member variables via initializer lists.
I wish to have a parametrized constructor which only updates one of the member variables. A simple example is below, with name
being the only variable that I would like to initialize via the default constructor.
Does it make sense to repeat the initializer list the same as for the default constructor here? Or can I call the default constructor from the parameterized constructor first?
What is the most efficient way to do this?
#include <iostream>
#include<iostream>
#include<fstream>
using namespace std;
class MyClass
{
public:
MyClass() : name("John"),
age(20),
distance(10)
// Many more default initializers
{
}
MyClass(string name)
{
// How to only initialize name with the parameter passed, while everything else still gets the default value like in the default constructor.
}
~MyClass()
{
}
private:
string name;
int age;
int distance;
// Many more
};
There are different ways to accomplish this:
#include<iostream>
#include<fstream>
using namespace std;
class MyClass
{
public:
MyClass() : MyClass("John", 20, 10)
{
}
MyClass(string name): MyClass(name, 20, 10)
{
}
~MyClass()
{
}
MyClass(std::string pName, int pAge, int pDistance):
name(pName),
age(pAge),
distance(pDistance)
{}
private:
string name;
int age;
int distance;
// Many more
};
Initialize private members
#include<iostream>
#include<fstream>
using namespace std;
class MyClass
{
public:
MyClass()
{
// using the default values
}
MyClass(string pName): name(name)
{
// update the value of name and keep other set to default
}
~MyClass()
{
}
MyClass(std::string pName, int pAge, int pDistance):
name(pName),
age(pAge),
distance(pDistance)
{
// if needed, you can have another constructor to change the default values
}
private:
string name = std::string("default name");
int age = 0;
int distance = 0;
// Many more
};
The optimum solution depends on your requirements/usage.
For example, if you go for the second option, any change in the default values will trigger a rebuild on all files including the class definition.