move constructor

1
2
3
4
5
6
7
8
9
class MyString
{
// pick implementation from Listing 9.9
};
MyString Copy(MyString& source) // function
{
MyString copyForReturn(source.GetString()); // create copy
return copyForReturn; // return by value invokes copy constructor
}

A deep copy was performed twice, Copy(sayHello) return a MyString by value but this value returned is very temporary not available outside this expression. the copy constructor invoked in good faith by C++ compiler is a burden on performance. To avoid it, move constructor introduced:

1
2
3
4
5
6
7
8
9
//move constructor
MyString (MyString &&moveSource)
{
if (moveSource.buffer != NULL)
{
buffer = moveSource.buffer;//take ownership i.e. 'move'
moveSource.buffer = NULL;//set the move source to NULL
}
}

when using move constructor compiler avoiding a deep-copy step.

class not permit copy (private copy constructor)

1
2
3
4
5
6
class President
{
private:
President(const president&);//private copy constructor
President & operator = (const President&);//private copy assignment operator
}

Singleton class President that prohibits copying, assignment, and multiple instance creation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <string>
using namespace std;
class President
{
private:
President(){};
President(const President &);
const President &operator=(const President &);

string name;

public:
static President &GetInstance()
{
static President onlyInstance;
return onlyInstance;
}

string GetName()
{
return name;
}

void SetName(string InputName)
{
name = InputName;
}
};
int main()
{
President &onlyPresident = President::GetInstance();
onlyPresident.SetName("Abraham Lincoln");

// President second; // cannot access constructor
// President* third= new President(); // cannot access constructor
// President fourth = onlyPresident; // cannot access copy constructor
// onlyPresident = President::GetInstance(); // cannot access operator=

cout << "The name of the President is: ";
cout << President::GetInstance().GetName() << endl;

return 0;
}

Lines 35 and 36 try object creation on the stack and free store, respectively, using the default constructor, which is unavailable because it’s private, as declared in Line 7.

static members

some operator of class in c++

Operators Symbols
dot operator .
arrow operator ->
scope resolution operator ::
  • dot operator:

    accessing members using dot operator, eg:

    1
    2
    3
    Human firstMan;
    firstMan.dateOfBirth="xxxx";
    firstMan.IntroduceSelf();

    note that: dot operator can be used both inside class and outside class.

  • arrow operator:(difference between . and -> )

    accessing members using arrow operator, if an object has been instantiated on the free store using new or having a pointer to an object, eg:

    1
    2
    3
    4
    Human *firstWoman = new Human ();
    firstWoman->dateOfBirth="xxxx";
    firstWoman->IntroduceSelf();
    delete firstWoman;

    note that: dot operator can be used both inside class and outside class.

  • scope resolution operator

    1. To access a global variable when there is a local variable with same name

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      #include<iostream>
      using namespace std;
      int x; // Global x
      int main()
      {
      int x = 10; // Local x
      cout << "Value of global x is " << ::x;
      cout << "\nValue of local x is " << x;
      return 0;
      }
    2. To define a function outside a class.

      1
      2
      3
      4
      5
      class A {
      public:
      void fun();
      };
      void A::fun() { cout << "fun() called"; }

    3. To access a class’s static variables.

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      class Test
      {
      static int x;
      public:
      static int y;
      void func(int x)
      {
      cout << "Value of static x is " << Test::x;
      cout << "\nValue of local x is " << x;
      }
      };

      int Test::x = 1;
      int Test::y = 2;

      int main()
      {
      Test obj;
      int x = 3 ;
      obj.func(x);

      cout << "\nTest::y = " << Test::y;

      return 0;
      }

constructors with initialization lists

another way to initialize members is by using initialization lists. eg:

1
2
3
4
5
6
7
8
9
10
11
12
13
class Human
{
private:
int age;
string name;
public:
Human(string humanName, int humanAge)
:name(humanName),age(humanAge)//constructor
{
//...content
}
//...other class
};

another eg:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Human
{
private:
string name;
int age;

public:
Human(string yourName = "eve", int yourAge = 25)
: name(yourName), age(yourAge)
{
cout << age << " and " << name << endl;
}
};

int main()
{
Human firstWoman;
Human firstMan(18, "Adam");

return 0;
}

from line 17 note that if you already initialize object, firstWoman don't include () which in this case eg: Human firstWoman() this is wrong Human firstWoman is correct.

using constructor to convert types

considering an class Human that feature an overloaded constructor that accepts an integer, eg:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Human
{
int age;
public:
Human(int humanAge): age(humanAge)
{
//initialization lists
}
};
void DoSomething(Human person)
{
cout << "Human sent did something" << endl;
return;
}

int main()
{
Human kid(10);//convert integer in to a Human
DoSomething(kid);

return 0;
}

whereas implicit conversions, eg:

1
2
Human otherKid = 10; //int converted to Human
DoSomething(10); //10 convert to Human

supplement

To avoid implicit conversions, use keyword explicit at the time of declaring the constructor.

eg:

1
2
3
4
5
6
7
8
9
class Human
{
int age;
public:
explicit Human (int humanAge) : age(humanAge)
{
//...content
}
};

using explicit is not a prerequisite but in many case a good programming practice.