C++ - Pointer, Reference, Virtual Methods and Struct

C++ is one of the most widely used programming languages in the world. It is very flexible as one can work both at high-level abstraction and low-level hardware features. Here we will briefly talk about Pointer, Reference, Virtual Methods, and Struct. I will also try to keep this post updating :).

1. Pointer and Reference

In C++, objects (variables, classes, …) are ALL value type except using an explicit reference (T&) or a pointer (T*). As value types are copyable, when assigning an instance of a class to another instance, the assignment is done member-wisely. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class A{

public:
int member;
};

A a;
A b;
a.member = 5;
b = a; // will copy values of every member to b : b.member = a.member
cout << a.member << endl; // 5
cout << b.member << endl; // 5

b.member = 10; // only change b.member
cout << a.member << endl; // 5
cout << b.member << endl; // 10

If one wants a reference type of object, one can take use of reference (T&) or a pointer (T*). Reference and pointer both store the address of a variable which is located elsewhere in the memory. The difference between them is that reference can be seen as an alias of an existing variable, and it cannot be changed to refer to another object or null after it is initialized. In short, reference is like a constant pointer.

Additionally, a pointer itself has its own memory address and size on the stack, while a reference shares the same memory address with the variable it refers to (but it still takes up some space on the stack).

1
2
3
4
5
6
7
8
9
10
int i = 3; 

int *ptr = &i; // ptr is a pointer to variable i or "stores the address of i"
int &ref = i; // &ref is reference for i, and it is the same as &i. ref is the same as i or 3

std::cout << p << std::endl; // address of i: 0x7fff96ac6384
std::cout << *p << std::endl; // extract the value which p points to: 3
std::cout << &i << std::endl; // address of i: 0x7fff96ac6384
std::cout << ref << std::endl; // the value which &ref refers: 3
std::cout << &ref << std::endl; // reference of i: 0x7fff96ac6384

2. Virtual Methods, Virtual Table and Virtual Pointer

When virtual method is defined by a class or a base class, the compiler will create a virtual pointer and virtual table for the class. Virtual pointer is usually located at the first place in the object memory, and points to the virtual table (the first virtual method in the virtual table).

The virtual table saves all the addresses of the virtual methods with the same order as they are defined. When inheriting, virtual methods of the derived class are placed in the virtual table of the first base class. It means that the virtual table should have the addresses of the virtual methods in the base class itself (if the derived class overrides, then only save the overrided methods from the derived class) and the new virtual methods in the derived class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Base
{
public:
VirtualTable* __vptr; // set to Base's virtual table when an object of Base is created
// Base's virtual table: address of function1 and fuction2 in Base class
virtual void function1() {};
virtual void function2() {};
};

class A: public Base
{
public:
// inherit * __vptr from Base // set to A's virtual table when an object of A is created
// A's virtual table: address of function1 in A class and fuction2 in Base class
void function1() override {};
};

class B: public Base
{
public:
// inherit * __vptr from Base // set to B's virtual table when an object of B is created
// A's virtual table: address of function1 in Base class and fuction2 in B class
void function2() override {};
};

3. Struct and Class

There are very small differences between structs and classes in C++. The only difference is that members in structs are by default public while members in classes are by default private. But both structs and classes can have private, public, or protected members.

When inheriting, structs are public inheritance by default and classes default to private inheritance.