`

c++ - copy constructor and the initializer constructor in container and Array

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

we know you can define constructor and the copy constructor for a user defined class, and if you faile to provide one, the compiler will be able to generate one for you.  The generated ones are called default constructor or default copy constructor, the former will call the default constructor for its member and its base classes, and the later will call the default memberwise copy constructor for its member if the member failes to provide one or the copy constructor if it has provided one, and its applies the same as regard to the base classes object parts.

 

but how it works if we are create a array of user define classes or a container like vector of user defined classes?

 

let's see an example.

 

#include "stdafx.h"

#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

using std::string;
using std::cout;
using std::endl;
using std::vector;
using std::copy;
using std::inserter;


class CustomClass 
{
public: 
	CustomClass(string name_, int age_) : name(name_), age(age_) {
	  cout << "inside CustomClass(string name, int age)" << endl;
	}
	CustomClass(const CustomClass& rhs) : name(rhs.name), age(rhs.age) {
	  cout << "inside CustomClass(const CustomClass& rhs)" << endl;
	}
private:
	std::string name;
	int age;
};

void TestVectorCopy()
{
	//vector<CustomClass> customClasses(4) = 
	// the results shows that it will first call the constructor with (string, int), then with the const CustomClass& - the copy constructor
	CustomClass customClasses[] = { CustomClass("joe", 29), CustomClass("Yun", 30), CustomClass("Cliff", 31), CustomClass("Nora", 31), CustomClass("Tom", 30) }; 

	vector<CustomClass> v_customClasses;

	v_customClasses.reserve(sizeof(customClasses) / sizeof(CustomClass));

	// as you can see, there are in total const CustomClasses&  called when copy from the CustomClass[] to the vector<CustomClass>
	copy(customClasses, customClasses + 4, inserter(v_customClasses, v_customClasses.begin())); 

	// as you will find in the following code, another CustomClasses is called
	vector<CustomClass> v_customClasses2 = v_customClasses;

	vector<CustomClass> v_customClasses3;
	v_customClasses.reserve(v_customClasses2.size());
	copy(v_customClasses.cbegin(), v_customClasses.cend(), inserter(v_customClasses3, v_customClasses3.begin()));

}

 

the result shows that copy constructor is called for each element of container, and the constructor is called for each of the initializer value of the first array.

分享到:
评论

相关推荐

    Android代码-Watchface-Constructor

    (https://yalantis.com/blog/building-a-customizable-constructor-for-designers-of-android-wear-watch-face-apps) This is simple watchface constructor demo. It offers customization of the following ...

    boa-constructor入门介绍

    boa-constructor是一款基于Python语言,使用wxPython库构建图形用户界面(GUI)的应用程序开发工具。它为非专业程序员提供了一种直观、易用的可视化界面来设计和编写GUI程序,降低了开发复杂度,尤其适合初学者入门...

    Copy Constructors and Assignment Operators终极解释

    在C++编程语言中,复制构造函数(Copy Constructor)和赋值运算符(Assignment Operator)是两个非常关键的概念,特别是在处理对象的拷贝和赋值时。它们默认由编译器提供,但通常需要根据具体需求进行自定义,以确保正确...

    Inside the C++ object model 高清英文版

    oriented programming within C++: constructor semantics, temporary generation, support for encapsulation, inheritance, and "the virtuals"-virtual functions and virtual inheritance. This book shows how ...

    setter-on-constructor-prototype.rar_The Next

    在JavaScript编程中,"setter-on-constructor-prototype.rar_The Next"这个主题涉及到对象属性的访问和修改,尤其是关于构造函数原型链上的setter方法。在描述中提到的"Get the name of the next property of Result...

    boa-constructor-0.6.1.src.win32

    boa-constructor-0.6.1.src.win32.exe

    boa-constructor-0.6.1.bin

    boa-constructor-0.6.1.bin.setup

    boa-constructor-0.6.1.bin.setup

    boa-constructor-0.6.1.bin.setup 这是WINDOWS安装版本。也是最新的。不容易找啊。 这是第一个包

    Google C++ Style Guide_英文版.pdf

    - Variable and Array Initialization - Preprocessor Directives - Class Format - Constructor Initializer Lists - Namespace Formatting - Horizontal Whitespace - Vertical Whitespace - **Exceptions ...

    prototypal-oo-js-object-oriented-constructor-functions-lab-onlin

    标题中的“prototypal-oo-js-object-oriented-constructor-functions-lab-onlin”表明这是一个关于JavaScript中原型式面向对象编程(Prototype-based Object-Oriented Programming)的实验或练习,特别是涉及构造...

    Inside the C++ object model

    oriented programming within C++: constructor semantics, temporary generation, support for encapsulation, inheritance, and "the virtuals"--virtual functions and virtual inheritance. This book shows how...

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

    Tabs Function Declarations and Definitions Function Calls Conditionals Loops and Switch Statements Pointer and Reference Expressions Boolean Expressions Return Values Variable and Array Initialization...

    (完整word)重庆大学c++-A卷.doc

    重庆大学C++考试试卷解析 本资源为重庆大学C++考试试卷,涵盖面向...本资源涵盖了C++中的多个知识点,包括引用、指针、晚绑定、copy constructor、访问修饰符、名字装饰、函数调用、变量作用域、类的继承和多态等。

    Inside the C++ Object Model

    oriented programming within C++: constructor semantics, temporary generation, support for encapsulation, inheritance, and "the virtuals"-virtual functions and virtual inheritance. This book shows how ...

    Addison.Wesley.C++.by.Dissection.2002.pdf

    - **Constructors:** Explains constructor definitions, including default constructors, initializer lists, and copy constructors. - **Destructors:** Describes destructor definitions. - **Members That ...

    boa-constructor-0.6.1. for python2.5

    "boa-constructor-0.6.1 for Python2.5" 是一个专为Python 2.5版本设计的集成开发环境(IDE),主要用于图形化构建应用程序的用户界面(UI)。Boa Constructor是一款开源软件,它提供了对Python程序员来说非常友好的...

    node---Copy.zip_class_node

    在这个主题中,"node---Copy.zip_class_node" 提到的是一个关于链表(Linked List)的数据结构实现,尤其是与Node类相关的部分。链表是一种线性数据结构,其元素(节点)不是在内存中连续存储,而是通过指针相互链接...

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    - FIX: In "Windows ClearType" font rendering mode (OS Windows mode) the "garbage" pixels can appear from the right and from the bottom sides of the painted rectangle of the TFlexText object....

    java反射之Constructor

    Java 反射之 Constructor Java 反射机制中的 Constructor 类提供了关于类的单个构造方法的信息,包括对它的访问权限、Class 获取构造函数的方式等。下面将详细讲解 Constructor 类的概述、获取构造函数的方式、构造...

Global site tag (gtag.js) - Google Analytics