- 浏览: 399497 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (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)
最新评论
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(); // ... }
发表评论
-
不安装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 ...
相关推荐
在C++Builder中,动态创建控件是一种常见的编程技术,特别是在设计用户界面时,需要根据用户的操作或程序的运行状态灵活地添加、删除或修改控件。本篇将深入探讨如何在C++Builder中实现动态创建控件数组,并提供相关...
在C++中,如果在比较中使用的常量超出范围,会出现constant out of range in comparison错误。在比较中,需要确保常量在合法的范围内。 Conversion may lose significant digits(转换时会丢失意义的数字) -------...
1.1 C++模式模式(The C++ Object Model) 简单对象模型(A Simple Object Model) 表格驱动对象模型(A Table-driven Object Model) C++对象模型(The C++ Object Model) 对象模型如何影响程序(How the Object ...
使用`GetObjectArrayElement`获取元素,并且如果需要修改元素,可能需要通过`NewObject`创建新对象并使用`SetObjectArrayElement`设置回原数组。需要注意的是,对于对象数组,内存管理更需要注意,防止Java垃圾回收...
- **C++对象模型 (The C++ object model)**:描述了C++中对象的表示方式。 - **程序执行 (Program execution)**:规定了程序执行的基本原则。 - **多线程执行和数据竞争 (Multi-threaded executions and data ...
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 ...
9. **托管数组与非托管数组**:C++/CLI支持两种类型的数组,一种是.NET的托管数组(如System::Array),另一种是C++风格的非托管数组。两者的内存管理方式和使用方式有所不同。 10. **互操作性(Interoperability)...
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 ...
- **1.8 The C++ object model C++ 对象模型**:阐述了 C++11 对象的生命周期和内存布局。 - **1.9 Program execution 程序执行**:描述了程序执行的基本过程及其行为。 - **1.10 Multi-threaded executions and ...
- **引用类型**:探讨如何创建和使用引用类型,特别是内置类型如`Array`和`Object`。 - **数组的识别**:介绍如何检测一个变量是否为数组,这对于处理复杂数据结构至关重要。 #### 第二章:函数 JavaScript中的...
- Access to any object inside your application (in case you've allowed this). Standard libraries to access to base classes, controls and forms. Easily expandable library architecture. - Debugger....
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 ...
Cannot modify a const object ---------------不允许修改常量对象 Case outside of switch ----------------漏掉了case 语句 Case syntax error ------------------ Case 语法错误 Code has no effect ---------...
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....
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 ...
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 - ...
This chapter introduces object-oriented programming (OOP) concepts in C++: - **Classes and Objects**: Explanation of classes and objects, including encapsulation, inheritance, and polymorphism. - **...
5. **Array size too large** - 数组大小超过了C++允许的最大值,通常与内存限制有关。 6. **Bad character in parameters** - 在参数中使用了非法字符,如非字母数字字符或保留字符。 7. **Bad file name format ...
- **C++对象模型 (C++ Object Model)**:定义了C++中对象的基本属性和行为,以及它们是如何存储和访问的。 - **程序执行 (Program Execution)**:描述了程序的执行流程和控制流。 - **多线程执行和数据竞争 (Multi-...
`<array>`头文件定义了固定大小的数组类array,它是C风格数组的现代替代品,提供了更安全和便利的操作。 在并发和多线程编程中,`<atomic>`头文件提供了原子操作,确保数据在并发环境下的完整性。`...