Member function | typical form for class C : |
---|---|
Default constructor | C::C(); |
Destructor | C::~C(); |
Copy constructor | C::C (const C&); |
Copy assignment | C& operator= (const C&); |
Move constructor | C::C (C&&); |
Move assignment | C& operator= (C&&); |
|
|
Example
has a default constructor. Therefore, objects of this class can be constructed by simply declaring them without any arguments:
|
|
|
|
int
. Therefore the following object declaration would be correct:
|
|
|
|
|
|
bar's content: Example |
Example3
has a default constructor (i.e., a constructor without parameters) defined as an empty block:
|
|
Example3
to be constructed without arguments (like foo
was declared in this example). Normally, a default constructor like this is implicitly defined for all classes that have no other constructors and thus no explicit definition is required. But in this case, Example3
has another constructor:
|
|
void
. It also uses the class name as its own name, but preceded with a tilde sign (~
):
|
|
bar's content: Example |
Example4
allocates storage for a string
. Storage that is later released by the destructor.foo
and bar
this happens at the end of function main
.const
qualified) and which can be invoked with a single argument of this type. For example, for a class MyClass
, the copy constructor may have the following signature:
|
|
|
|
|
|
Example4
we defined above, because it contains pointers of which it handles its storage. For that class, performing a shallow copy means that the pointer value is copied, but not the content itself; This means that both objects (the copy and the original) would be sharing a single string
object (they would both be pointing to the same object), and at some point (on destruction) both objects would try to delete the same block of memory, probably causing the program to crash on runtime. This can be solved by defining the following custom copy constructor that performs a deep copy:
|
|
bar's content: Example |
|
|
baz
is initialized on construction using an equal sign, but this is not an assignment operation! (although it may look like one): The declaration of an object is not an assignment operation, it is just another of the syntaxes to call single-argument constructors.foo
is an assignment operation. No object is being declared here, but an operation is being performed on an existing object; foo
.operator=
which takes a value or reference of the class itself as parameter. The return value is generally a reference to *this
(although this is not required). For example, for a class MyClass
, the copy assignment may have the following signature:
|
|
Example5
. In this case, not only the class incurs the risk of deleting the pointed object twice, but the assignment creates memory leaks by not deleting the object pointed by the object before the assignment. These issues could be solved with a copy assignment that deletes the previous object and performs a deep copy:
|
|
string
member is not constant, it could re-utilize the same string
object:
|
|
|
|
fn
and the value constructed with MyClass
are unnamed temporaries. In these cases, there is no need to make a copy, because the unnamed object is very short-lived and can be acquired by the other object when this is a more efficient operation.
|
|
&&
). As a parameter, an rvalue reference matches arguments of temporaries of this type.
|
|
foo's content: Example |
Member function | implicitly defined: | default definition: |
---|---|---|
Default constructor | if no other constructors | does nothing |
Destructor | if no destructor | does nothing |
Copy constructor | if no move constructor and no move assignment | copies all members |
Copy assignment | if no move constructor and no move assignment | copies all members |
Move constructor | if no destructor, no copy constructor and no copy nor move assignment | moves all members |
Move assignment | if no destructor, no copy constructor and no copy nor move assignment | moves all members |
default
and delete
, respectively. The syntax is either one of:function_declaration = default; function_declaration = delete; |
|
|
bar's area: 200 |
Rectangle
can be constructed either with two int
arguments or be default-constructed (with no arguments). It cannot however be copy-constructed from another Rectangle
object, because this function has been deleted. Therefore, assuming the objects of the last example, the following statement would not be valid:
|
|
|
|
|
|
default
does not define a member function equal to the default constructor (i.e., where default constructor means constructor with no parameters), but equal to the constructor that would be implicitly defined if not deleted.delete
or default
on the other special member functions they don't explicitly define.Previous: Classes (II) | Index | Next: Friendship and inheritance |