C + + Tutorial local variable, reference, constructor after Colon

Source: Internet
Author: User

The following syntax is encountered, after which the variable is assigned a value

class a{
int a;
int b;
public : int int bb): a (aa), b (bb) { }}
Equivalent to:
int int Bb) {a=aa;b=bb;}

Code location:

#include <iostream>using namespacestd;classa{ public: A (intMsg) {                 this->msg =msg; cout<<"Create a\n"; }        ~A () {cout<<"Destroy A\n"; }        intmsg;};voidSwap (a& A, a&B) {A Temp=a; A=b; b=temp;}intmain () {a A (1); A B (2); Std::cout<<"a="<<a.msg<<"b="<<b.msg;        Swap (a, b); Std::cout<<"a="<<a.msg<<"b="<<b.msg; Std::cout<<"Hellow world"; //will A be destroyed?         return 0;}

1 Local variable creation

Create objects on the stack directly within a scope a (corresponding parameterless Constructor) and a a (1) (with parametric constructors)

2 References to issues

C's function if not with a reference is passed by value! The following function

void Swap (* a, A * b) {        a* temp = a        ; = b;         = temp;}

It is useless to write a swap object like This.

Since the pointer is a value passed, the Swap function is left as usual

  

C + + Tutorial local variable, reference, constructor after Colon

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.