Example of direct initialization and replication initialization of C + + classes __c++

Source: Internet
Author: User

(1) For general built-in types, there is basically no difference between the two types of initialization.

int a (5);//Direct initialization

int a=5;//Copy Initialization

int A=int (5);//Direct initialization

(2) When used for class-type objects, the initialization form differs from the direct form:

Direct initialization directly invokes the constructor that matches the argument, and replication initialization always calls the copy constructor.

Replication initialization first creates a temporary object with the specified constructor, and then uses the copy constructor to copy that temporary object to the object being created.

account.h********************************************* *******************
#ifndef account_h
#define ACCOUNT_H

#include <string> class
Account {//A simple bank accounts class
public :
	enum Acctype{nul = 0,PRV = 1,ENP = 2};//account type, non-private
	enum account& display (Std::ostream &os);//Show Account
	Account (const std::string &ow = ""): Owner (OW), Amount (0.0), Actyp (NUL) {}//constructor with default arguments (with default constructor) account
	(const std::string &ow,double Am,acctype Act)://Constructor
		owner (OW) with three formal parameters, amount (AM), Actyp (Act) {}
private:
	std::string owner;//account name
	double amount;//balance
	acctype actyp;//account type
};

Inline account& accounts::d isplay (std::ostream &os) 
{//Output account information
	os << "Owner:" <<owner << "Amount:" << Amount << "Type: << actyp << Std::endl;
	return *this;
}

#endif
main.cpp********************************************** ******************

#include <iostream> #include <vector> #include "Account.h"

using namespace Std;

Account Example (account ACC);

the int main (void) {account null_acc;////directly initializes, calling the default constructor directly

Account MYACC ("Tlj", 500,account::P RV);//Direct initialization, direct call to the constructor matching the arguments Myacc.display (cout);

Account test; Test = MYACC;

Account Null_account = string ("");//or ... = account ()//copy initialization, but do not copy constructors, create Null_account.display (cout) with default constructors only;

Account Err_account = string ("#");//copy initialization, but do not copy constructors, create Err_account.display (cout) with default constructors only;

Account SAME_ACC = myacc;//replication initialization, calling the copy constructor with account SAME_ACC (MYACC), but only if the copy constructor is declared explicit (Same_acc.display) ;

Account Func_ret = Example (MYACC);//copy initialization, return a copy of AA to Func_ret Func_ret.display (cout);  Account *PACC = new Account (MYACC)//Use the copy constructor, initialize an anonymous object with MYACC replication, and assign the address of the new object to the PACC pointer. Pacc->display (cout);

Vector<account> avec (5);//copy initialization, first create a temporary value with the account constructor to initialize the avec, and then use the copy constructor to copy the temporary values to each element of avec for (vector< Account>::iterator it = Avec.begin (); It!= avec.end ();  ++it) {it->display (cout); }

Account aaray[] = {string ("A"), String ("B"), String ("C"),};//replication initialization, first create temporary object with constructor, and then copy to elements with copy constructor for (int i = 0; I!= 4;  ++i) {aaray[i].display (cout); }

return 0; }

Account Example//uses the copy constructor to copy the arguments to ACC. NOTE: Formal parameters and return values are not reference types, and no person will not use the copy constructor {account AA (ACC);//copy initialization to copy ACC to AA ... return aa;//uses a copy constructor, returning a copy of AA

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.