`

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

相关推荐

    dynamic-create-control-array.rar_C++builder

    在C++Builder中,动态创建控件是一种常见的编程技术,特别是在设计用户界面时,需要根据用户的操作或程序的运行状态灵活地添加、删除或修改控件。本篇将深入探讨如何在C++Builder中实现动态创建控件数组,并提供相关...

    c++错误解释(比较常见的错误类型)

    在C++中,如果在比较中使用的常量超出范围,会出现constant out of range in comparison错误。在比较中,需要确保常量在合法的范围内。 Conversion may lose significant digits(转换时会丢失意义的数字) -------...

    《深度探索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 ...

    JNI-array-arg.rar_JNI-array-arg_jni_jni 数组_jni array

    使用`GetObjectArrayElement`获取元素,并且如果需要修改元素,可能需要通过`NewObject`创建新对象并使用`SetObjectArrayElement`设置回原数组。需要注意的是,对于对象数组,内存管理更需要注意,防止Java垃圾回收...

    C++ Standard - ANSI ISO IEC 14882 2011

    - **C++对象模型 (The C++ object model)**:描述了C++中对象的表示方式。 - **程序执行 (Program execution)**:规定了程序执行的基本原则。 - **多线程执行和数据竞争 (Multi-threaded executions and data ...

    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 ...

    C++/CLI in Action source code

    9. **托管数组与非托管数组**:C++/CLI支持两种类型的数组,一种是.NET的托管数组(如System::Array),另一种是C++风格的非托管数组。两者的内存管理方式和使用方式有所不同。 10. **互操作性(Interoperability)...

    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 ...

    C++11最新使用手册

    - **1.8 The C++ object model C++ 对象模型**:阐述了 C++11 对象的生命周期和内存布局。 - **1.9 Program execution 程序执行**:描述了程序执行的基本过程及其行为。 - **1.10 Multi-threaded executions and ...

    .Principles.of.Object-Oriented.Programming.in.JavaScript

    - **引用类型**:探讨如何创建和使用引用类型,特别是内置类型如`Array`和`Object`。 - **数组的识别**:介绍如何检测一个变量是否为数组,这对于处理复杂数据结构至关重要。 #### 第二章:函数 JavaScript中的...

    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....

    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 ...

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

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

    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....

    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 ...

    绿色版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 - ...

    Problem Solving with C++ (7th edition)

    This chapter introduces object-oriented programming (OOP) concepts in C++: - **Classes and Objects**: Explanation of classes and objects, including encapsulation, inheritance, and polymorphism. - **...

    C++错误一览表(中英文对照)

    5. **Array size too large** - 数组大小超过了C++允许的最大值,通常与内存限制有关。 6. **Bad character in parameters** - 在参数中使用了非法字符,如非字母数字字符或保留字符。 7. **Bad file name format ...

    c++17 标准文档

    - **C++对象模型 (C++ Object Model)**:定义了C++中对象的基本属性和行为,以及它们是如何存储和访问的。 - **程序执行 (Program Execution)**:描述了程序的执行流程和控制流。 - **多线程执行和数据竞争 (Multi-...

    C++ 标准库 中文 高清 (2020最新带书签)

    `&lt;array&gt;`头文件定义了固定大小的数组类array,它是C风格数组的现代替代品,提供了更安全和便利的操作。 在并发和多线程编程中,`&lt;atomic&gt;`头文件提供了原子操作,确保数据在并发环境下的完整性。`...

Global site tag (gtag.js) - Google Analytics