8.1. Initializing Class Member Variables
Problem
You need to initialize member variables that are native types, pointers, or references.
Solution
Use an initializer list to set the initial values for member variables. Example 8-1 shows how you can do this for native types, pointers, and references.
Example 8-1. Initializing class members
#include using namespace std; class Foo { public: Foo[] : counter_[0], str_[NULL] {} Foo[int c, string* p] : counter_[c], str_[p] {} private: int counter_; string* str_; }; int main[] { string s = "bar"; Foo[2, &s]; }
Discussion
You should always initialize native variables, especially if they are class member variables. Class variables, on the other hand, should have a constructor defined that will initialize its state properly, so you do not always have to initialize them. Leaving a native variable in an uninitialized state, where it contains garbage, is asking for trouble. But there are a few different ways to do this in C++, which is what this recipe discusses.
The simplest things to initialize are native types. int
s, char
s, pointers, and so on are easy to deal with. Consider
a simple class and its default constructor:
class Foo { public: Foo[] : counter_[0], str_[NULL] {} Foo[int c, string* p] : counter_[c], str_[p] {} private: int counter_; string* str_; };
Use an initializer list in the constructor to initialize member variables, and avoid doing so in the body of the constructor. This leaves the body of the constructor for any logic ...
C# 3.0 [.NET 3.5] introduced Object Initializer Syntax, a new way to initialize an object of a class or collection. Object initializers allow you to assign values to the fields or properties at the time of creating an object without invoking a constructor. public class Student
{
public int StudentID { get; set; }
public string StudentName { get; set; }
public int Age { get; set; }
public string Address { get; set; }
}
class Program
{
static void Main[string[] args]
{
Student std = new Student[] { StudentID = 1,
StudentName = "Bill",
Age = 20,
Address = "New York"
};
}
}
In the above example, Student class is defined without any constructors. In the Main[] method, we have created Student object and assigned values to all or some properties in the curly bracket at the same time. This is called object initializer syntax.
The compiler compiles the above initializer into something like the following.
Student __student = new Student[];
__student.StudentID = 1;
__student.StudentName = "Bill";
__student.Age = 20;
__student.StandardID = 10;
__student.Address = "Test";
Student std = __student;
Collection Initializer Syntax
Collection can be initialized the same way as class objects using collection initializer syntax.
var student1 = new Student[] { StudentID = 1, StudentName = "John" };
var student2 = new Student[] { StudentID = 2, StudentName = "Steve" };
var student3 = new Student[] { StudentID = 3, StudentName = "Bill" } ;
var student4 = new Student[] { StudentID = 3, StudentName = "Bill" };
var student5 = new Student[] { StudentID = 5, StudentName = "Ron" };
IList studentList = new List[] {
student1,
student2,
student3,
student4,
student5
};
You can also initialize collections and objects at the same time.
IList studentList = new List[] {
new Student[] { StudentID = 1, StudentName = "John"} ,
new Student[] { StudentID = 2, StudentName = "Steve"} ,
new Student[] { StudentID = 3, StudentName = "Bill"} ,
new Student[] { StudentID = 3, StudentName = "Bill"} ,
new Student[] { StudentID = 4, StudentName = "Ram" } ,
new Student[] { StudentID = 5, StudentName = "Ron" }
};
You can also specify null as an element:
IList studentList = new List[] {
new Student[] { StudentID = 1, StudentName = "John"} ,
null
};
Advantages of Initializers
- Initializer syntax makes a code more readable, easy to add elements into the collection.
- Useful in multi-threading.