Reputation: 4771
In my code I am doing the following, but I am not sure if I am "allowed" to or if it is a good designing technique. I need to create an empty constructor, but I also need a constructor that initializes the variables given the parameters. So I am doing the following:
This is the C.h file.
class C
{
private:
string A;
double B;
public:
//empty constructor
C();
C(string, double);
}
And my C.cpp file:
//this is how I declare the empty constructor
C::C()
{
}
C::C(string a, double b)
{
A = a;
B = b;
}
Is the way I am declaring the empty constructor right or do I need to set A= NULL and B=0.0?
Upvotes: 13
Views: 61048
Reputation: 1296
In C++11 and later you can use the following to generate a default no-param constructor:
C() = default;
This is neater than C(){}.
This doesn't initialize members. In C++11 you can initialize members in the same line of declaration:
int m_member = 0; // this is a class member
Those 2 features avoids having to create your own no param constructor to default initialize members. So your class can look like this when applying those 2 features:
class C
{
private:
string A;
double B = 0;
public:
C() = default;
C(string, double);
}
Upvotes: 9
Reputation: 76245
You are the only person who can answer this question, because it depends entirely on your requirements for a default-constructed object. Since you haven't mentioned what your requirements are, it's not possible to give a definitive answer. Some people will guess that you should initialize B
to 0, but that decision should be based on your design, not on various notions of "good" programming practice.
Upvotes: 2
Reputation: 227370
Your empty constructor does not do what you want. The double
data member will not be zero-initialized unless you do it yourself. The std::string
will be initialized to an empty string. So the correct implementation of the default constructor would simply be
C::C() : B() {} // zero-initializes B
Concerning the other constructor, you should prefer the initialization list:
C::C(const string& a, double b) : A(a), B(b) {}
otherwise, what you are doing is an assignment to default constructed objects.
Upvotes: 17
Reputation: 18443
It is fine to do this and leave the constructor empty, but you should be aware that uninitialized fields have undefined value. string
is a class and it's default constructor takes care of its initialization, but double
is not initialized here (in your defualt constructor), and its value is undefined (it may be whatever value previously exists in the memory).
Upvotes: 5
Reputation: 3407
You are doing it correct and you can see it by compiling your code. Your default constructor will initialize the string and double by calling their default constructors. If you define any constructor in your class it will hide the default constructor created by compiler for you.
You can improve your constructor code by writing it like this
C::C(string a, double b):A(a), b(b)
{
}
Upvotes: 0
Reputation: 45410
A is std::string
, you can't set it to NULL
but can set to empty string and std::string
has default constructor which initialize it to empty string by default.
C::C()
:B(0.0)
{
}
Maybe you need a constructor constructor with default parameter instead of two constuctors?
C(const string& a= "", double b= 0.0)
: A(a),
B(b)
{
}
Upvotes: 3
Reputation: 53871
You can leave it as is. You can do this because both string
and double
can be default constructed. Meaning that you can say string foo;
and not get any errors.
Contrast this with what happens here:
class Bar{
private:
Bar(); //Explicitly private;
};
Bar b;
Here we get an error about no constructor Bar::Bar()
being found.
As to whether it's a good idea: It's hard to say without knowing the situation this class will be used in. Perhaps it's perfectly sensible to have it be in a unconfigured position. But for many classes, such as a class representing a file for example, allowing a file object which doesn't point to any file is obviously wrong.
Upvotes: 1