Implicitly calling constructors
-
I have been playing with PC-lint and reading the Scott Meyers Effective/More Effective C++ books and have a question about implicitly calling constructors. This may be a style issue, or it may turn out to be something more important. For example, suppose you have a class that contains a
std::string
(orCString
), and the strings should be empty ("") when the class is created. I have always let the compiler implicitly call the string constructor, and hence would have something that looks like this:class foo
{
private:
std::string str_;
public:
foo() /* str_ constructor implicitly called */
{
}
};However, it has been suggested that all members should appear in the constructors initialization list, and that I should change my constructor to one of the following:
foo() : str_("")
or
foo() : str_()
Which of the abiove you use? Any solid reasons why? Also, in the case of a string, would you explicitly initialize it to an empty string (
str_("")
) or call the strings default constructor (str_()
)?
Kicking squealing Gucci little piggy.
-
I have been playing with PC-lint and reading the Scott Meyers Effective/More Effective C++ books and have a question about implicitly calling constructors. This may be a style issue, or it may turn out to be something more important. For example, suppose you have a class that contains a
std::string
(orCString
), and the strings should be empty ("") when the class is created. I have always let the compiler implicitly call the string constructor, and hence would have something that looks like this:class foo
{
private:
std::string str_;
public:
foo() /* str_ constructor implicitly called */
{
}
};However, it has been suggested that all members should appear in the constructors initialization list, and that I should change my constructor to one of the following:
foo() : str_("")
or
foo() : str_()
Which of the abiove you use? Any solid reasons why? Also, in the case of a string, would you explicitly initialize it to an empty string (
str_("")
) or call the strings default constructor (str_()
)?
Kicking squealing Gucci little piggy.
Rob Caldecott wrote:
Which of the abiove you use? Any solid reasons why? Also, in the case of a string, would you explicitly initialize it to an empty string (str_("")) or call the strings default constructor (str_())?
Since
str_("")
doesn't serve any better purpose thatstr_()
I would go for str_(). Since a good default constructor will set it's member elements to a predefined state. This should suffice. :)
Nibu thomas A Developer Programming tips[^] My site[^]
-
Rob Caldecott wrote:
Which of the abiove you use? Any solid reasons why? Also, in the case of a string, would you explicitly initialize it to an empty string (str_("")) or call the strings default constructor (str_())?
Since
str_("")
doesn't serve any better purpose thatstr_()
I would go for str_(). Since a good default constructor will set it's member elements to a predefined state. This should suffice. :)
Nibu thomas A Developer Programming tips[^] My site[^]
calling str_() would save on having to pass a parameter. (not much of a difference, just me being picky).
-
I have been playing with PC-lint and reading the Scott Meyers Effective/More Effective C++ books and have a question about implicitly calling constructors. This may be a style issue, or it may turn out to be something more important. For example, suppose you have a class that contains a
std::string
(orCString
), and the strings should be empty ("") when the class is created. I have always let the compiler implicitly call the string constructor, and hence would have something that looks like this:class foo
{
private:
std::string str_;
public:
foo() /* str_ constructor implicitly called */
{
}
};However, it has been suggested that all members should appear in the constructors initialization list, and that I should change my constructor to one of the following:
foo() : str_("")
or
foo() : str_()
Which of the abiove you use? Any solid reasons why? Also, in the case of a string, would you explicitly initialize it to an empty string (
str_("")
) or call the strings default constructor (str_()
)?
Kicking squealing Gucci little piggy.
Rob Caldecott wrote:
For example, suppose you have a class that contains a std::string (or CString), and the strings should be empty ("") when the class is created. I have always let the compiler implicitly call the string constructor, and hence would have something that looks like this: class foo{private: std::string str_;public: foo() /* str_ constructor implicitly called */ { }};
you are right.
Rob Caldecott wrote:
However, it has been suggested that all members should appear in the constructors initialization list, and that I should change my constructor to one of the following: foo() : str_("") or foo() : str_()
In this case it doesn't make sense to initialize member variable in initialization list. Because, generally, well designed classes takes care of default initialization. Only difference, here would hapeen is, if you doesn't use member initialization list, default c'tor will be called, and copy c'tor otherwise. It is advised to use member initialization list, because it save assignment operator call; if you are going to assign some value to member variable. consider this scenario,
class foo
{
CStrin m_sMyString;
public:
foo()
{
m_sMyString = "This string i want as default"; //assignment operator
//here m_sMyString is initialized to default value,and again,assigned to new
// value using assignment operator
}};
where as this is efficient,
class foo
{
CStrin m_sMyString;
public:
foo(): m_sMyString("This string i want as default")
{
//here m_sMyString is initialized to value we want
}};
Prasad Notifier using ATL | Operator new[],delete[][^]
-
I have been playing with PC-lint and reading the Scott Meyers Effective/More Effective C++ books and have a question about implicitly calling constructors. This may be a style issue, or it may turn out to be something more important. For example, suppose you have a class that contains a
std::string
(orCString
), and the strings should be empty ("") when the class is created. I have always let the compiler implicitly call the string constructor, and hence would have something that looks like this:class foo
{
private:
std::string str_;
public:
foo() /* str_ constructor implicitly called */
{
}
};However, it has been suggested that all members should appear in the constructors initialization list, and that I should change my constructor to one of the following:
foo() : str_("")
or
foo() : str_()
Which of the abiove you use? Any solid reasons why? Also, in the case of a string, would you explicitly initialize it to an empty string (
str_("")
) or call the strings default constructor (str_()
)?
Kicking squealing Gucci little piggy.
See member initialization list for more.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
See member initialization list for more.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
OK - the order of initialization is important, etc. but I am still unsure whether it is good practice to initialize all members, even those (like strings for example), that already have default constructors that can be called implicitly. What do you do in your code for example? Do you initialize everything or just POD types?
Kicking squealing Gucci little piggy.
-
I have been playing with PC-lint and reading the Scott Meyers Effective/More Effective C++ books and have a question about implicitly calling constructors. This may be a style issue, or it may turn out to be something more important. For example, suppose you have a class that contains a
std::string
(orCString
), and the strings should be empty ("") when the class is created. I have always let the compiler implicitly call the string constructor, and hence would have something that looks like this:class foo
{
private:
std::string str_;
public:
foo() /* str_ constructor implicitly called */
{
}
};However, it has been suggested that all members should appear in the constructors initialization list, and that I should change my constructor to one of the following:
foo() : str_("")
or
foo() : str_()
Which of the abiove you use? Any solid reasons why? Also, in the case of a string, would you explicitly initialize it to an empty string (
str_("")
) or call the strings default constructor (str_()
)?
Kicking squealing Gucci little piggy.
-
OK - the order of initialization is important, etc. but I am still unsure whether it is good practice to initialize all members, even those (like strings for example), that already have default constructors that can be called implicitly. What do you do in your code for example? Do you initialize everything or just POD types?
Kicking squealing Gucci little piggy.
Rob Caldecott wrote:
Do you initialize everything or just POD types?
Only those that I know for sure don't have their own initialization.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb