- 浏览: 389771 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (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 15288首先你需要下载的是 Microsoft Windows S ... -
rpath - runtime search path
2013-04-03 11:36 965RPath is a very interesting to ... -
C++ - autogenerated copy constructor and assignment operator gotchas
2013-01-24 13:32 744It has been changed that the s ... -
c++ - rethrow a exception gotchas
2012-12-23 10:57 923As in my prevoius example in j ... -
c++ -typeid operator
2012-10-15 22:30 1033typeid is the one of the meager ... -
c++ - dynamic_cast revisit
2012-10-14 21:21 738There are several built-in type ... -
c++ - virtual inheritance example 1
2012-10-14 15:25 784we have discussed the virtual i ... -
c++ - virtual inheritance
2012-10-12 08:58 915As we have discussed in the pos ... -
c++ type of inheritance
2012-09-28 08:58 724There are 3 types of inheritanc ... -
c++ - vritually virtual new
2012-09-27 23:59 932Let's see what if we want to cl ... -
c++ - virtual destructor
2012-09-27 22:01 926As we all know that virtual des ... -
c++ - vritual function and default arguments
2012-09-27 08:56 951As we all know that we virtual ... -
c++ - template specialization and partial specialization
2012-09-26 22:38 1289in this post, we are going to e ... -
c++ - member template in class template
2012-09-26 08:19 907class member template can be us ... -
c++ template class and the pattern to use its friends
2012-09-25 23:47 960template class may defined thei ... -
c++ - Friend declaration in class Template
2012-09-25 08:47 1183There are three kinds of friend ... -
c++ - class template default parameters
2012-09-25 08:18 806the template has parameter, it ... -
c++ - operator new and delete and an example of linked list stores by new/delete
2012-09-24 07:53 555The operator new and delete ope ... -
c++ - delete(void *, size_t) or delete(void *)
2012-09-24 07:18 1136In my previous dicuss, we have ... -
c++ - placement operator new() and the operator delete()
2012-09-23 15:22 839A 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 ...
oriented programming within C++: constructor semantics, temporary generation, support for encapsulation, inheritance, and "the virtuals"--virtual functions and virtual inheritance. This book shows how...
oriented programming within C++: constructor semantics, temporary generation, support for encapsulation, inheritance, and "the virtuals"-virtual functions and virtual inheritance. This book shows how ...
boa-constructor-0.6.1.src.win32.exe
prototypal-oo-js-object-oriented-constructor-functions-lab-online-web-ft-110419-源码.rar
boa-constructor-0.6.1.bin.setup 这是WINDOWS安装版本。也是最新的。不容易找啊。 这是第一个包
boa-constructor-0.6.1.bin.setup
There are plenty of good books written on the subject, but I found no clear and concise set of rules on the Internet for those who don't want to understand every nuance of the language—and just want...
Boa-constructor 是一个很好的wxpython gui设计软件
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...
oriented programming within C++: constructor semantics, temporary generation, support for encapsulation, inheritance, and "the virtuals"-virtual functions and virtual inheritance. This book shows how ...
Get the name of the next property of Result(3) that doesn t have the DontEnum attribute.
boa-constructor-0.6.1. for python2.5 支持拖拽的ui IDE
- 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....
Inside the C++ Object Model focuses on the underlying mechanisms that support object-oriented programming within C++: constructor semantics, temporary generation, support for encapsulation, ...
eslint-plugin-no-constructor-bind 优先使用类属性,而不要使用类的构造函数方法中的等效设置步骤。 // Bad: class User { constructor ( ) { this . greet = this . greet . bind ( this ) } greet ( ) { ...
Copy Constructor and Object Cloning Class Member Access Class member offsets Function Pointers Function Shadowing Understanding the Destructor Operator Overloading Multiple Inheritance Casting ...
boa-constructor-0.4.4.win32 for python2.3.X wxpython 2.5.4.1