How do you initialize a member object in C++?
Show 8.1. Initializing Class Member VariablesProblemYou need to initialize member variables that are native types, pointers, or references. SolutionUse 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 DiscussionYou 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. 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. 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.
Collection Initializer SyntaxCollection can be initialized the same way as class objects using collection initializer syntax.
You can also initialize collections and objects at the same time.
You can also specify null as an element:
Advantages of Initializers
How data members of an object can be initialized?Object Initialization :
We have seen how variables can be initialized at the time of declaration, an example of this is: int sum = 0 ; Here variable 'sum' is declared and initialized with the value 0. Variable initialization is important so that we do not accidently use a variable without assigning a value to it.
What is member initialization?Member initializer list is the place where non-default initialization of these objects can be specified. For bases and non-static data members that cannot be default-initialized, such as members of reference and const-qualified types, member initializers must be specified.
What are the 3 ways of object initialization?There are 3 ways to initialize object in Java.. By reference variable.. By method.. By constructor.. Should member variables be initialized?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.
|