In my previous post - we have discussed on the topic of c++ - copy constructor and the initializer constructor in container and array.
Now, since we have conver the destructor and construcotr , we know there is default constructor, copy constructor, constructor that is marked explicit, and we know a single argument constructor can be be think of the conversion constructor...
now let's revisit the array/object initializer...below is the code and the comment inside it shall be enough to tell the key point of the object initializer ....
/** * file * array_initializer.cpp * description: * This shows how to use the array initializer to apply the array initialization */ #include "stdafx.h" #include "acct.h" #include <iostream> using std::cout; using std::endl; using std::cerr; void test_array_initializer() { cout << "initialize with the default single argument" << endl; // if object only has one single explicit constructor argument ,you can do this Account pooh_pals_single_args[] = { "piglet", "Eeyore", "Tigger" }; // and of course, if you want to specify mutiple arguments, here is the way // ther is one misunderstood in that // if will first call the // Account ("piglet", 1000.0); // and then the copy constructor will be called // Account(const Account & rhs); // the misconcept is that it will first create the temporary objects, which are // in the { ... } // block, then pooh_pals_mutiple_args will be invoked to get the right object contructed. // cout << "initialize with the multiple arguments" << endl; Account pooh_pals_multiple_args[] = { Account("piglet", 1000.0), Account("Eeyore", 1000.0), Account("Tigger", 1000.0) }; // if you want to initialize some with the default constructor, here is the way how you do it cout << "initialize with the multiple arguments mixed with default argument" << endl; Account pooh_pals_multiple_args_with_default_constructor[] = { Account("piglet", 1000.0), Account("Eeyore", 1000.0), Account(), }; // and if you specify the size, but in the initializer list, there is no insufficient number of // object in the initializer list, // then the default constructor will be called cout << "Default constructor will be called if the initializer list number is smaller than the array/contaier size" << endl; Account pooh_pals_multiple_args_with_insufficient_objects[3] = { Account("piglet", 1000.0), Account("Eeyore", 1000.0), }; // while if you are calling the new opertor, there is no way to specify the initializer with arguments cout << "default constructo will be called new operator on the heap" << endl; Account * pact = new Account[10]; // delete[] pact; }
and below is the definition of the Account class's header file.
class Account { public: /* ================= * ctor and dtors ========================*/ Account(); // since a constructor that takes only a single parameter can serves as a conversion operator // we don't want tripwire people into the trap of // Account acct = "new user"; // to help curb/reign this kind of unwantted/unwelcome compiler aid, the explicit keyword is introduced // //explicit //Account(const char *, double = 0.0); // for the purpose of demo the use of the array initalizer, we decide to use the implicit constructor approach Account(const char *, double = 0.0); // the copy constructor // the most important thing about the copy constructor is to decide whether or not to provide the copy constructor // the how as to implements the copy constructor comes seconds Account(const Account & ); ~Account(); const char * name(); // ... }