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