- 浏览: 399505 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (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)
最新评论
The following classes show many a apsect of object life-cycle management, including the single argument constructor, copy constructor, default constructor, expclit constructor, destructor and the non-static member and static member, and how to define/impl those classes
We might use the definition on this chapter later for other illustration.
/** * file * acct.h * description: * the class definition of the Account class * */ 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(); private: char * _name; unsigned int _acct_nmbr; double _balance; static unsigned int _unique_acct_nmbr; static unsigned int get_unique_acct_nmbr(); static void return_acct_nmbr(unsigned int acct_nmbr); protected: };
and below is the definition/implementatio of the classes..
/** * file * acct.cpp * description: * the implementation source file of the acct.h */ #include "stdafx.h" #include "acct.h" #include <iostream> #include <typeinfo> #include <exception> #include <fstream> #include <functional> #include <algorithm> #include <vector> #include <list> #include <array> #include <iterator> #include <cstring> #include <memory> using std::cout; using std::endl; using std::end; using std::cerr; using std::fstream; using std::vector; using std::list; using std::strcpy; using std::strcat; using std::strlen; using std::auto_ptr; /*================================= * ctor(s) and dtor(s) ==================================*/ // for non-class members, it does not matter if you initialize the non-class member in the initializer list of you initialize the // the non-class member inside the constructor's body // the convention is to initialize the non-class members inside the ctor's body, while initialize the class members in the initializer's initializer list Account::Account() { _balance = 0.0; _name = 0; _acct_nmbr = 0; _acct_nmbr = get_unique_acct_nmbr(); cout << "Account::Account" << endl; } // As said before,you can initialize the non-class member in the initliazer list. //Account::Account() : // _balance (0.0), _name(0), _acct_nmber(0) //{ //} // though the declaration of the Account is as such // Account(const char * naem_, double balance_ = 0.0); // but in our discussion, we can NOT redefine the default value of the balance_ Account::Account(const char * name_, double balance_) { cout << "Account::Account(const char * name_, double balance_)" << endl; if (name_ != NULL) { // thereis no need to call the delete operator because // in the intializer, the name_ is guaranteed to be filled with som randome bitness // while the static members will be filled with zero or its default values (normally it is zeros) //delete name_; _name = new char[strlen(name_) + 1]; strcpy(_name, name_); } _balance = balance_; _acct_nmbr = get_unique_acct_nmbr(); } Account::Account(const Account& rhs) : _balance(rhs._balance) { cout << "Account::Account(const Account & rhs)" << endl; _name = new char[strlen(rhs._name) + 1]; strcpy(_name, rhs._name); // must copy rhs._acct_nmbr _acct_nmbr = get_unique_acct_nmbr(); } unsigned int Account::_unique_acct_nmbr; unsigned int Account::get_unique_acct_nmbr() { return ++_unique_acct_nmbr; } void Account::return_acct_nmbr(unsigned int acct_nmbr) { } inline const char * Account::name() { return _name; } // Destructor server primarily to relinquish resources acquired either within the constructor or urnig the lifetime // thelifetime of the class object, again such as freeing a mutal exclsion lock or deleting memory allocated through operator new. Account::~Account() { delete _name; return_acct_nmbr(_acct_nmbr); // there is no need to do the check // because the compiler will ensure that. //if (_name != NULL ) delete _name; // and since the object is destructed, there is no need to rest to some uninitialized value. //_name = 0; //_balance = 0; //_acct_nmbr = 0; } Account global; void destructor_life_cycle_test() { Account local("Anna live Plurablle", 1000); Account &loc_ref = global; auto_ptr<Account> pact(new Account("stephen Dedalus")); { Account local_too("Stephen Hero"); // the local_too will destroy before exit the current local space. } // the auto_ptr will destruct here }
发表评论
-
不安装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 ...
相关推荐
It is the first C++ book that actually demonstrates how to design large systems, and one of the few books on object-oriented design specifically geared to practical aspects of the C++ programming ...
开发工具 spring-aspects-4.3.6.RELEASE开发工具 spring-aspects-4.3.6.RELEASE开发工具 spring-aspects-4.3.6.RELEASE开发工具 spring-aspects-4.3.6.RELEASE开发工具 spring-aspects-4.3.6.RELEASE开发工具 spring...
Both an introduction to the theoretical aspects of automated object/relational mapping and a practical guide to the use of Hibernate, this book provides extensive example code to implement an online ...
Scaling a Django application requires careful consideration of various aspects such as caching, load balancing, and database management. **Key Concepts:** - **Caching Strategies:** Implementing ...
Although it is based on C++, Java is more of a “pure” object-oriented language. Both C++ and Java are hybrid languages, but in Java the designers felt that the hybridization was not as important as ...
"C++/CLI: The Visual C++ Language for .NET introduces Microsoft's extensions to the C++ syntax that allow you to target the common language runtime the key to the heart of the .NET 3.0 platform....
Modern C++ Programming Cookbook ...This book follows a recipe-based approach, with examples that will empower you to implement the core programming language features and explore the newer aspects of C++.
automatic initialization to prevent another class of bugs. Chapter ten introduces delegates and shows how classes and callback functions cooperate to simplify, for example, the constant chore of ...
interpolation of components to a common resolution; application of registration offsets in the CRG marker segment; and colour conversion to an appropriate rendering space (sRGB here). The "kdu_...
/xscjManager/WebContent/WEB-INF/lib/spring-aspects.jar /xscjManager/WebContent/WEB-INF/lib/spring-beans.jar /xscjManager/WebContent/WEB-INF/lib/spring-context.jar /xscjManager/WebContent/WEB-INF/lib/...
3.2.7/mybatis-spring-1.2.2/mysql-connector-java-5.1.7-bin/slf4j-api-1.7.5/slf4j-log4j12-1.7.5/spring-aop-4.1.3.RELEASE/spring-aspects-4.1.3.RELEASE/spring-beans-4.1.3.RELEASE/spring-context-4.1.3....
Python Package to Visualize different aspects of a Neural Network
The book is structured into chapters that cover various aspects of C++, from the basics to more advanced topics. Here, we will summarize the key points and concepts from each chapter: ### Chapter 1:...
This book presents some of the important aspects of the C++/CLI language that often become a barrier preventing programmers from exploring further. The C++/CLI Primer is a powerful but compact book ...
《深入剖析Spring Aspects——基于源码的探索》 Spring Aspects是Spring框架的一个重要组成部分,它提供了面向切面编程(AOP)的功能,使得开发者能够以声明式的方式管理横切关注点,如日志、事务管理等。本文将...
2.5.2 Representation of a Neuron as an Input-Dictated Cybernetic Regulator with a Quadratic Cost-function 2.5.3 Generalized Information-Theoretic Entropy Measure 2.5.4 Influence of Nonlinear ...
This book presents some of the important aspects of the C++/CLI language that often become a barrier preventing programmers from exploring further. The C++/CLI Primer is a powerful but compact book ...
is related to C and C++ but is organized rather differently, with a number of aspects of C and C++ omitted and a few ideas from other languages included. It is intended to be a production language, ...