Terminology

Terminology

Reference: Effective C++ Third Edition 55 Specific Ways to Improve Your Programs and Designs

Terminology

Declaration: Tells compilers about the name and type of something, but not details.

1
2
3
4
5
6
7
8
extern int x; // object declaration

// std defines c++ standard library
// size_t both exists in C and C++
// signature of a function: return type and its parameter types
std::size_t numDigits(int number) // func declaration
class Widget;
template<typename T> class GraphNode(T smallT) // template declaration

Definition: provides compilers with the details a declaration omits. It is where compilers set aside memory for the object.

1
2
3
4
5
int x; // object definition
std::size_t numDigits(int number){
// function code
}
...

Initialisation:

  • process of giving an object its first value.
  • For objects of user-defined types -> initialisation performed by constructors.
  • Default Constructor: can be called without any arguments or has default values.

Explicit:

  • it prevents from being used to perform implicit type conversions, but may be used for explicit type conversions.
  • Explicit constructors are more preferable than non-explicits as it prevents compilers from performing unexpected type converstions.
  • If you dont have good reason to do implicit type conversion -> declare it as explicit!
1
2
3
4
5
6
class B{
explicit B(int x= 0, bool b = true);
};
void doSomething(B object); // function takes B as an input

doSomething(B(28)); // explicit type convert for int 28 to object B

Copy Constructor: copy the another object of the same type.
Copy Assignment Operator: to copy the value from one object to another of the same type.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class A{
public:
A(); // default constructor
A(const A& rhs); // copy constructor
A& operator=(const A& rhs); // copy assignment operator
};

A a1; // Invoke def constr
A a2(a1); // Invoke copy constr
a1 = a2; // Invoke copy assignment operator
A a3 = a2; // Invoke copy constructor

void tempFunc(A a); //
tempFunc(a3); // a3 passed by value -> copy constructor called a = a3.

How to distinguish? If a new object is being defined like a2 and a3, it is copy constructor. If an existing object copies another like a1 = a2, it is copy assignment constructor.

Possible undefined behavrious:

1
2
3
4
5
int *p = 0; // p is a nullptr
std::cout << *p; // dereferencing a nullptr -> undefined

char name[] = "sixcha"; // array of size 7 including trailing null
char c = name[10]; // Referencing invalid array index.
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×