All of the constructors methods here do the same thing. I mostly use method2 but saw method3 for the first time today. Have seen method1 in some places but dont know what are the exact differences between them ? Which one is the best way to define constructors and why ? Is there any performance issues involved ?
1 class Test
2 {
3 private:
4 int a;
5 char *b;
6 public:
7 Test(){};
8
9 // method 1
10 Test(int &vara, char *& varb) : a(vara), b(varb){}
11
12 // method 2
13 Test(int &vara, char *& varb)
14 {
15 a = vara;
16 b = varb;
17 }
18
19 //method 3
20 Test(int &vara, char *& varb)
21 {
22 this->a = vara;
23 this->b = varb;
24 }
25
26 ~Test(){}
27 };
I have here used simple fields int and char*,what will happen if I have many fields or complex types like struct ??
Thanks