- 浏览: 399506 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (309)
- xaml C# wpf (0)
- scala java inner clas (1)
- Tools UML Eclipse UML2 (1)
- Timer .NET Framework (1)
- perl (6)
- python function paramter (1)
- Python Docstring (1)
- Python how to compare types (1)
- Python (8)
- java (5)
- C# (76)
- C# WPF (0)
- p4 (0)
- WPF (46)
- .net (6)
- xaml (1)
- javascript (40)
- windows (10)
- scala (4)
- winform (1)
- c++ (48)
- tools (12)
- cmd (1)
- os (0)
- CI (0)
- shell (0)
- C (2)
- haskell (49)
- functional (1)
- tool (1)
- gnu (1)
- linux (1)
- kaskell (0)
- svn (0)
- wcf (3)
- android (1)
最新评论
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.
发表评论
-
不安装Visual Studio,只用Windows SDK搭建VC环境
2013-12-31 21:52 15342首先你需要下载的是 Microsoft Windows S ... -
rpath - runtime search path
2013-04-03 11:36 1012RPath is a very interesting to ... -
C++ - autogenerated copy constructor and assignment operator gotchas
2013-01-24 13:32 771It has been changed that the s ... -
c++ - rethrow a exception gotchas
2012-12-23 10:57 960As in my prevoius example in j ... -
c++ -typeid operator
2012-10-15 22:30 1060typeid is the one of the meager ... -
c++ - dynamic_cast revisit
2012-10-14 21:21 771There are several built-in type ... -
c++ - virtual inheritance example 1
2012-10-14 15:25 823we have discussed the virtual i ... -
c++ - virtual inheritance
2012-10-12 08:58 977As we have discussed in the pos ... -
c++ type of inheritance
2012-09-28 08:58 754There are 3 types of inheritanc ... -
c++ - vritually virtual new
2012-09-27 23:59 960Let's see what if we want to cl ... -
c++ - virtual destructor
2012-09-27 22:01 975As we all know that virtual des ... -
c++ - vritual function and default arguments
2012-09-27 08:56 994As we all know that we virtual ... -
c++ - template specialization and partial specialization
2012-09-26 22:38 1328in this post, we are going to e ... -
c++ - member template in class template
2012-09-26 08:19 939class member template can be us ... -
c++ template class and the pattern to use its friends
2012-09-25 23:47 986template class may defined thei ... -
c++ - Friend declaration in class Template
2012-09-25 08:47 1212There are three kinds of friend ... -
c++ - class template default parameters
2012-09-25 08:18 854the template has parameter, it ... -
c++ - operator new and delete and an example of linked list stores by new/delete
2012-09-24 07:53 588The operator new and delete ope ... -
c++ - delete(void *, size_t) or delete(void *)
2012-09-24 07:18 1170In my previous dicuss, we have ... -
c++ - placement operator new() and the operator delete()
2012-09-23 15:22 873A class member operator new() c ...
相关推荐
(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是一款基于Python语言,使用wxPython库构建图形用户界面(GUI)的应用程序开发工具。它为非专业程序员提供了一种直观、易用的可视化界面来设计和编写GUI程序,降低了开发复杂度,尤其适合初学者入门...
在C++编程语言中,复制构造函数(Copy Constructor)和赋值运算符(Assignment Operator)是两个非常关键的概念,特别是在处理对象的拷贝和赋值时。它们默认由编译器提供,但通常需要根据具体需求进行自定义,以确保正确...
oriented programming within C++: constructor semantics, temporary generation, support for encapsulation, inheritance, and "the virtuals"-virtual functions and virtual inheritance. This book shows how ...
在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.exe
boa-constructor-0.6.1.bin.setup
boa-constructor-0.6.1.bin.setup 这是WINDOWS安装版本。也是最新的。不容易找啊。 这是第一个包
- 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”表明这是一个关于JavaScript中原型式面向对象编程(Prototype-based Object-Oriented Programming)的实验或练习,特别是涉及构造...
oriented programming within C++: constructor semantics, temporary generation, support for encapsulation, inheritance, and "the virtuals"--virtual functions and virtual inheritance. This book shows how...
Tabs Function Declarations and Definitions Function Calls Conditionals Loops and Switch Statements Pointer and Reference Expressions Boolean Expressions Return Values Variable and Array Initialization...
重庆大学C++考试试卷解析 本资源为重庆大学C++考试试卷,涵盖面向...本资源涵盖了C++中的多个知识点,包括引用、指针、晚绑定、copy constructor、访问修饰符、名字装饰、函数调用、变量作用域、类的继承和多态等。
oriented programming within C++: constructor semantics, temporary generation, support for encapsulation, inheritance, and "the virtuals"-virtual functions and virtual inheritance. This book shows how ...
- **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" 是一个专为Python 2.5版本设计的集成开发环境(IDE),主要用于图形化构建应用程序的用户界面(UI)。Boa Constructor是一款开源软件,它提供了对Python程序员来说非常友好的...
在这个主题中,"node---Copy.zip_class_node" 提到的是一个关于链表(Linked List)的数据结构实现,尤其是与Node类相关的部分。链表是一种线性数据结构,其元素(节点)不是在内存中连续存储,而是通过指针相互链接...
- 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 类提供了关于类的单个构造方法的信息,包括对它的访问权限、Class 获取构造函数的方式等。下面将详细讲解 Constructor 类的概述、获取构造函数的方式、构造...