move constructor
1 | class MyString |
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 | //move constructor |
when using move constructor compiler avoiding a deep-copy step.
class not permit copy (private copy constructor)
1 | class President |
Singleton class President
that prohibits copying, assignment, and multiple instance creation.
1 |
|
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.
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
3Human 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
4Human *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
To access a global variable when there is a local variable with same name
1
2
3
4
5
6
7
8
9
10
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;
}To define a function outside a class.
1
2
3
4
5class A {
public:
void fun();
};
void A::fun() { cout << "fun() called"; }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
26class 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 | class Human |
another eg:
1 | class Human |
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 | class Human |
whereas implicit conversions, eg:
1 | Human otherKid = 10; //int converted to Human |
supplement
To avoid implicit conversions, use keyword
explicit
at the time of declaring the constructor.
eg:
1 | class Human |
using explicit
is not a prerequisite but in many case a good programming practice.