📖
Go C++
  • Introduction
  • Chapter 1: What You Must Know First
    • Virtual Address Space of Process: Memory Partition and Layout
    • Function Call: Stack Frame
    • Program Compiling and Linking
  • Chapter 2: C++ Basics Improvement
    • Default Parameters
    • Inline Function
    • Function Overloading
    • new and delete
    • const and Pointers
    • References in Detail
  • Chapter 3: Object-Oriented Principles
  • Class and Object
  • Constructor and Destructor
  • Shallow Copy and Deep Copy
  • Initializer List
  • Various Member Functions
  • Pointer to Class Members
  • Chapter 4: Template Programming
  • Function Templates
  • Class Templates
  • Memory Allocators
  • Chapter 5: Operator Overloading
    • Operator Overloading
    • Introduction to Iterators
    • Issues of Iterator Invalidation
    • More about new and delete
    • Overloading of new and delete: Object Pool
  • Chapter 6: Inheritance and Polymorphism
    • Look inside Inheritance
    • More about Inheritance
    • Virtual Functions, Static Binding and Dynamic Binding
    • More about Virtual Functions
    • Understanding Polymorphism
    • Abstract Classes
    • Frequently Asked Interview Questions: Polymorphism
  • Chapter 7: Multiple Inheritance
    • Virtual Inheritance and Virtual Base Classes
    • Diamond Problem
    • Four Kinds of Type Conversions
  • Chapter 8: Standard Template Library
    • Sequence Containers
    • Container Adaptors
    • Associative Containers
    • More about Iterators
    • Function Objects
    • Generic Algorithms, Binders and Lambda Expressions
  • Chapter 9: Object Optimization
    • Behind the Object
    • Optimizing Objects in Functions
    • Member Functions with Rvalue References
    • Move Semantics and Perfect Forwarding
  • Chapter 10: Smart Pointers
    • Smart Pointers
    • Smart Pointers without Reference Counting
    • Smart Pointers with Reference Counting
    • Custom Deleters
  • Chapter 11: Function Objects and Binders
    • More about Binders
    • Introduction to std::function
    • Template Specialization and Argument Deduction
    • More about std::function
    • std::bind(): A Simple Thread Pool
    • More about Lambda Expressions
  • Chapter 12: Multithreading
    • Important Features in C++11
    • Multithreaded Programming with std::thread
    • Mutual Exclusion
    • Producer-Consumer Problem
    • Atomic Operations
    • Thread Visibility and volatile
  • Chapter 13: Design Patterns
    • Singleton Pattern
    • Factory Pattern
    • Proxy Pattern
    • Decorator Pattern
    • Adapter Pattern
    • Observer Pattern
Powered by GitBook
On this page

Was this helpful?

Initializer List

Now let's go back to the Goods class we wrote before. Suppose we want to add a production date to the goods, with a Data class:

class Date {
 public:
  Date(int y, int m, int d) {
    _year = y;
    _month = m;
    _date = d;
  }
​
  void show() { 
      cout << _year << "/" << _month << "/" << _day << endl; 
  }
​
 private:
  int _yead;
  int _month;
  int _date;
}

Then we can make the data as one of the member attributes:

class Goods {
    ...
private:
      char _name[20];
      double _price;
      int _amount;
      Date _date;
}

But an error raises: Date does not have a default constructor. Remember that the declaration of an object includes both memory allocation and construction. We have already defined a customized constructor in Date, so there is no default constructor. But the constructor requires three parameters: year, month and date, which is provided when a Goods object is created. How can we pass them inside? A initializer list is used in initializing data members of a class. The list of members to be initialized is indicated with constructor as a comma-separated list followed by a colon:

Goods::Goods(const char *name, double price, int amount, int y, int m, int d) 
    : _date(y, m, d) {
  strcpy(_name, name);
  _price = price;
  _amount = amount;
}

In this way, y, m and d can be passed inside the constructor of Date. Not only can objects be initialized in this way, we can also use initializer list to initialize a normal member variable:

Goods::Goods(const char *name, double price, int amount, int y, int m, int d) 
    : _date(y, m, d), _price(price), _amount(amount) {
  strcpy(_name, name);
}

Notice that this initialization simply assigns values to variables, so _name has to be initialized inside the constructor with strcpy().

What's the initialization sequence of the initializer list? Does it follows the order from left to right? Let's look at this case.

class Test {
public:
    Test(int data = 10) : mb(_data), ma(mb) {}
    void show() {cout << "ma: " << ma << " mb " << mb << endl;}
private:
    int ma;
    int mb;
};
​
int main() {
    Test t;
    t.show();   // ma: -858993460 mb: 10
    return 0;
}

When t.show() is called, we find that mb is 10, but ma is a weird number. This is actually the default initial value in Visual Studio, which means that ma is not initialized correctly with mb. In fact, the initialization order only depends on the order we declared the variables, and has nothing to do with the initializer list. In the above case, we declared ma before mb, so ma is initialized first.

PreviousShallow Copy and Deep CopyNextVarious Member Functions

Last updated 4 years ago

Was this helpful?