`

c++ - object array intializer in c++

    博客分类:
  • c++
c++ 
阅读更多

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();
// ...
}
 
分享到:
评论

相关推荐

    Practical C++ Programming C++编程实践

    of-Line Puzzle Binary I/O Buffering Problems Unbuffered I/O Designing File Formats C-Style I/O Routines C-Style Conversion Routines C-Style Binary I/O C- Versus C++- Style I/O Programming Exercises ...

    Rad Studio Delphi C++builder XE 10.4 Patch2

    This patch addresses a number of issues in RAD Studio 10.4, pertaining to Delphi Compiler, the RAD Studio IDE in general and the new LSP-based Code Insight in particular, plus C++ Builder Android ...

    FastReport Professional v3.0 Full Source

    - Access to any object inside your application (in case you&#39;ve allowed this). Standard libraries to access to base classes, controls and forms. Easily expandable library architecture. - Debugger....

    A JSON parser in C++

    Perhaps because web service clients are usually written in dynamic languages these days, none of the existing C++ JSON parsers fitted my needs very well, so I wrote one that I used in another project....

    绿色版PocketDOS 和 绿色版TC3.0

    GETOPT C - Parses options in command line GREP2MSG C - Example program for Turbo C++ filters HELLO C - Example Turbo C++ program INTRO1 CPP - Example program from User's Guide INTRO10 CPP - ...

    C++ How to Program, 10th Edition

    C++ How to Program presents leading-edge computing technologies in a friendly manner appropriate for introductory college course sequences, based on the curriculum recommendations of two key ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    Header Files The #define Guard Header File Dependencies Inline Functions The -inl.h Files Function Parameter Ordering Names and Order of Includes Scoping Namespaces Nested Classes Nonmember, Static ...

    《深度探索C++对象模型》(Stanley B·Lippman[美] 著,侯捷 译)

    1.1 C++模式模式(The C++ Object Model) 简单对象模型(A Simple Object Model) 表格驱动对象模型(A Table-driven Object Model) C++对象模型(The C++ Object Model) 对象模型如何影响程序(How the Object ...

    C++出错提示英汉对照表

    Cannot modify a const object ---------------不允许修改常量对象 Case outside of switch ----------------漏掉了case 语句 Case syntax error ------------------ Case 语法错误 Code has no effect ---------...

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    - ADD: In the module FlexUtils added the constant BooleanWords - an array containing the words 'false' and 'true' for saving/reading TBoolProp. - ADD: In the module FlexUtils added the function ...

    C++ API for the JSON object interchange format

    CAJUN* is a C++ API for the JSON object interchange format. JSON is like XML, except it doesn't suck**. It is specifically designed for representing (in plain text format) structures familiar to ...

    numpy-1.16.4+mkl-cp36-cp36m-win_amd64.whl

    a powerful N-dimensional array object sophisticated (broadcasting) functions tools for integrating C/C++ and Fortran code useful linear algebra, Fourier transform, and random number capabilities ...

    Nucleus C++ v1.3 Upgrade

    support for the newer C++ array operators, new[] and delete[]. “C” callable allocation routines are supplied, including malloc()/free(). These routines are compatible in real-time with the C++ new ...

    Borland C++3.1 神话般的经典开发利器

    丰富的类库,本产品提供常用的数据结构,例如List, Queue, Qegue, Stack, Array. . 等。 12.丰富的函数库:本产品提供4, 5百个函数,可以说无所不包了。 1.2.1. Borland C++3.1新功能 1. 3.1版将Turbo C++ for ...

    From C to C++

    // A C++ Program without class and object! #include using namespace std; const int N=200; void strUpper(char *s); void strLower(char *s); int main(){ char ms[N]; cout; cin.getline(ms,N); ...

    file transfer using bluetooth

    a powerful N-dimensional array object sophisticated broadcasting functions basic linear algebra functions basic Fourier transforms sophisticated random number capabilities tools for integrating ...

    C++ by Example

    I see many of those students now moving to C++ in their school work or career. The C++ language is becoming an industry-accepted standard programming language, using the solid foundation of C to gain ...

    深度探索模C++对象模型PDF

    1.1 C++模式模式(The C++ Object Model) 简单对象模型(A Simple Object Model) 表格驱动对象模型(A Table-driven Object Model) C++对象模型(Th e C++ Object Model) 对象模型如何影响程序(How the Object ...

    深度探索C++对象模型 超清版

    1.1 C++模式模式(The C++ Object Model) 简单对象模型(A Simple Object Model) 表格驱动对象模型(A Table-driven Object Model) C++对象模型(Th e C++ Object Model) 对象模型如何影响程序(How the Object ...

Global site tag (gtag.js) - Google Analytics