Title: Theory / Unimportant / CS / Programming / C++ / Manual / Norm / 16 Classes
Tags: declaration todo_declaration seen_description automatedNewLineAfterCommaSpace computer_to_instruction syntactical_sugar

Instruction: class foo {
  bar
}
Page: 449
Meaning: Create a class foo using declarations in bar
Language: C++
Chapter: 16
Section: 1
Name: C++, Class definition
Extra: Sometime erroneously called "class declaration"

Name: A member function of C
Definition: A function declared within the definition of C
Page: 451
Chapter: 16
Section: 2.1
Variables: C a class
Context: Class

Instruction: type C::foo(bar) {
  plop
}
Page: 451
Meaning: Define the content of the member method foo of class C
Language: C++
Chapter: 16
Section: 2.1
Variables: C a class

Instruction: C c_ = c;
C c_ {c};
Page: 451
Effect: Copy the value of c into c_
Language: C++
Chapter: 16
Section: 2.1
Variables: C a class
c an instance of C

Question: What is the default copy behaviour for a class ?
Answer: Create a new class instance, where each member of the original instance are copied
Page: 451
Chapter: 16
Section: 2.1
Variables: C a class
Context: C++

Question: If some declaration is not declared public, private or protected, it is ?
Answer: private
Page: 451
Chapter: 16
Section: 2.1
Variables: C a class
Context: C++ Class

Name: A private member
Definition: A member which can be used only by other members of the same class
Page: 451
Chapter: 16
Section: 2.1
Variables: C a class
Context: C++ Class

Name: A public member
Definition: A member which can be used by everything
Page: 451
Chapter: 16
Section: 2.1
Variables: C a class
Context: Class

Question: Which of () or {} to prefer for initializing an object ?
Answer: {}; this way it is clear it's an initialization and not just a call
Page: 456
Chapter: 16
Section: 2.1
Variables: C a class
Context: Class definition

Instruction: C(types)
Page: 456
Meaning: Declare that C has an implicit constructor taking argument of type types
Language: C++
Chapter: 16
Section: 2.1
Context: Class definition
Name: Implicit constructor
Variables: C a class

Instruction: explicit C(types);
Page: 456
Meaning: Declare that C has an explicit constructor taking argument of type types.
Language: C++
Chapter: 16
Section: 2.1
Context: Class definition
Name: Explicit constructor
Variables: C a class

Question: What occurs if a value of type C is given where a value of type C' is expected ?
Answer: If there is an implicity constructor from C to C' then it is applied to the value of type C and the result is used
Page: 456
Chapter: 16
Section: 2.1
Context: C++

Name: Direct initalization
Definition: Initialization which is not done using an implicit initialization
Page: 458
Chapter: 16
Section: 2.1
Context: C++

Instruction: type var {expr};
Page: 460
Effect: Add a member var with type, if not explicitly initalized, then get value expr
Language: C++
Context: Class declaration
Chapter: 16
Section: 2.8

Instruction: C::C(params)
  :var_1{expr_1},..., var_n{expr_n} {
  bar
}
Page: 460
Meaning: Define a constructor for C with params, with members var_i taking value expr_i. Then executing bar
Language: C++
Chapter: 16
Section: 2.8
Context: Class declaration

Question: What is the consequence of adding a definition of a method directly in a class ?
Answer: This method is inlined
Page: 460
Chapter: 16
Section: 2.8
Context: Class declaration

Question: When is it acceptable to put a method definition in the class definition ?
Answer: Small method
Page: 460
Chapter: 16
Section: 2.8
Context: C++ Class declaration
Answer2: Rarely modified method
Answer3: frequently used method

Instruction: return_type function_name(
  arg_1,
  ...,
  arg_n
) const {
   ...
}
Page: 462
Meaning: Define a function whose method only uses consts function over its non-mutable members
Language: C++
Chapter: 16
Section: 2.9.1
Context: Class declaration
Intuition: The method does not change internal state

Name: Logical constness
Definition: The fact that a method seems consts to its user, independently on whether it is actually const
Page: 462
Chapter: 16
Section: 2.9.1
Context: Class declaration
Example: A method which compute its number of call and is otherwise consts
Example2: A method which cache the result of some computation in order to do it at most once

Instruction: mutable member_declaration
Page: 462
Meaning: Declares member_declaration while allowing it to be changed by const functions
Language: C++
Chapter: 16
Section: 2.9.1
Context: Class declaration

Question: What should be done with big mutable value
Answer: Create a class containing the mutable value
Page: 463
Chapter: 16
Section: 2.9.4
Context: Class

Question: Given a method changing some member, what should it returns ?
Answer: A reference to the object itself (so that the calls can be chained)
Page: 464
Chapter: 16
Section: 2.10
Context: Class

Name: this
Page: 465
Chapter: 16
Section: 2.10
Context: Class C

Instruction: this
Page: 465
Language: C++
Context: (Const) method
Chapter: 16
Section: 2.10
Returns: a pointer to the object for which the function was invoked.
Variables: Class C
Typ: (const) C* (rvalue)

Instruction: static before a declaration
Page: 467
Meaning: The declaration relates to the class and not to individual instances of the class
Language: C++
Chapter: 16
Section: 2.12
Context: Class C

Name: Member class
Definition: A class defined as a member of another class
Page: 467
Chapter: 16
Section: 2.12
Context: C++
Name2: Nested class

Name: A concrete class
Definition: A class whose representation is part of its definition
Page: 470
Chapter: 16
Section: 3
Context: C++
Name2: Value-type

Name: A helper function of Cl
Definition: A function whose meaning is related to Cl but is not in Cl itself
Page: 476
Chapter: 16
Section: 3
Context: Class C

Instruction: type operator@(params);
Page: 476
Meaning: Declaration of the operator @ over parameters params returning type
Language: C++
Chapter: 16
Section: 3
Context: Class C
Example: bool operator==(T a, T b)

Name: Value oriented programming
Definition: Programming using value type
Page: 476
Chapter: 16
Section: 3

Name: Copy from to to
Definition: Ensure that to has the same value than from and that from is unchanged
Page: 482
Chapter: 17
Section: 1
Context: C++

Name: move from to to
Definition: Ensure that to has the same previous value of from. The value of from is undefined
Page: 482
Chapter: 17
Section: 1

Name: Constructor
Definition: Similar to a function, without return type, instead set an object to it's initial state
Page: 484
Chapter: 17
Section: 2.1

Name: A ressource
Definition: Something that should be acquired and released
Page: 485
Chapter: 17
Section: 2.1

Instruction: C::~C();
Page: 485
Effect: The method called when an object of type C is destroyed
Language: C++
Chapter: 17
Section: 2.1
Variables: Class C

Typ: std::bad_alloc
Meaning: Exception for failing to allocate enough memory to create a new object
Page: 486
Language: C++
Chapter: 17
Section: 2.1
Variables: Class C

Question: What should be done for constructor in hierarchy:
Answer: Order: Base constructor, member constructor, then its body
Page: 486
Chapter: 17
Section: 2.1
Answer2: they should be virtual, in order to be

Question: What should be done for destructor in hierarchy
Answer: Order: Its destructor, member destructor, then its base class destructor
Page: 486
Chapter: 17
Section: 2.1
Answer2: It should be virtual if it has virtual function

Question: How to forbid implicit destruction
Answer: Make ~C private
Page: 488
Chapter: 17
Section: 2.1
Variables: C a class

Question: What does the default constructor ?
Answer: It initialize every values with their default value
Page: 489
Chapter: 17
Section: 2.5
Variables: C a class

Question: What does the constructor called without argument?
Answer: It leaves all elements unitialized
Page: 489
Chapter: 17
Section: 2.5
Variables: C a class

Name: Default constructor
Definition: The constructor which is called without argument
Page: 489
Chapter: 17
Section: 3.2
Variables: C a class
Context: C++

Name: Initializer-list constructor
Definition: The constructor whose argument's type is std::initializer_list
Page: 496
Chapter: 17
Section: 3.2
Variables: C a class

Question: In case of ambiguity, which constructor is used with {}
Answer: Default constructor if possible
Page: 496
Chapter: 17
Section: 3.2
Variables: C a class
Answer2: initialized_list constructor if possible
Answer3: Constructor with the same list of argument as input otherwise

Question: In which order are elements initalized in
C::C(params) : elt_1{expr_1}, ..., elt_n{expr_n} {}
Answer: In the order in which elements are defined in the class, and not in the order elt_1, ..., elt_n
Page: 501
Chapter: 17
Section: 4.1
Variables: C a class

Question: When are assignments mandatory ?
Answer: Const
Page: 501
Chapter: 17
Section: 4.1
Variables: C a class
Context: C++
Answer2: Reference

Question: How to call constructor of C from D ?
Answer: As if C were a member, C {value}
Page: 501
Chapter: 17
Section: 4.2
Variables: class C
class D : C

Name: Delegating constructor
Definition: A constructor which calls another constructor
Page: 503
Chapter: 17
Section: 4.2
Variables: class C
class D : C
Name2: Forwarding constructor

Instruction: C(plop): C {foo} {bar}
Page: 503
Effect: When the constructor is called with parameters plop, call constructor with arguments foo before executing the body bar
Language: C++
Chapter: 17
Section: 4.2
Variables: class C

Instruction: C(const C&)
Page: 507
Meaning: Apply this function when C {c}
Language: C++
Chapter: 17
Section: 4.2
Name: Copy constructor
Variables: class C

Instruction: C& operator=(const C&)
Page: 507
Meaning: Apply this function in expression as
C c_ = c
Language: C++
Chapter: 17
Section: 4.2
Name: Copy constructor
Variables: class C

Instruction: C& operator=(const C&&)
Page: 507
Meaning: Apply this function in expression as
C c_ = c
when moves occur
Language: C++
Chapter: 17
Section: 4.2
Name: move constructor
Variables: class C

Instruction: C(const C&&)
Page: 507
Meaning: Apply this function in expression as
C c_{c}
when moves occur
Language: C++
Chapter: 17
Section: 4.2
Name: move constructor
Variables: class C

Question: What should occur after copy
x=y
Answer: f(x)==f(y) for all functions f
Page: 510
Chapter: 17
Section: 5.1.3
Variables: class C
Answer2: any change to y does not affect x unless it refer to x, and reciprocally

Name: Copy-on-write
Definition: When A is copied to B, share the same resource until B is modified. When B is modified, do the actual copy. This ensure that data that are only read are not copied uselessly
Page: 512
Chapter: 17
Section: 5.1.3
Variables: class C

Name: Slicing
Definition: Copying an element of type D into an element of type C and losing some information (check)
Page: 513
Chapter: 17
Section: 5.1.3
Variables: class C
class D: C

Question: How to prevent slicing (copying derived class as if it were a base class)
Answer: delete the copy operations
Page: 514
Chapter: 17
Section: 5.1
Answer2: Prevent conversion of a pointer to a derived to a pointer to a base: make the base class a
private or protected base

Instruction: swap(
  var1,
  var2
)
Page: 514
Effect: exchange the values of var1 and var2, if possible using move instead of copy or other efficient method
Language: C++
Chapter: 17
Section: 5.1
Variables: type var1 var2;
Exception: noexcept if copy operations are noexcept

Name: A default operation
Definition: An operation that is generated automatically by the compiler instead of being explicitly written by the developer
Page: 517
Chapter: 17
Section: 5.1
Context: C++ class

Name: Implementation inheritance
Definition: to save implementation effort by sharing facilities provided by
a base class
Page: 578
Chapter: 20
Section: 1

Name: Interface inheritance
Definition: to allow different derived classes to be used interchangeably through
the interface provided by a common base class
Page: 578
Chapter: 20
Section: 1

Name: Derivation
Page: 579
Chapter: 20
Section: 1
Example: The process by which a derived class is related to its parent
Name2: Inheritance

Name: The class hierarchy
Page: 583
Chapter: 20
Section: 3
Context: C++
Example: The directed graph, with class ase nodes and edges from derived class to its base classes

Name: A function which override f
Definition: A function with the same name and argument as f in a class deriving D
Page: 587
Chapter: 20
Section: 3
Variables: C a class
f a member function
Context: C++
Assuming: f is virtual

Name: Polymorphism
Definition: The fact that the same method call can be executed by various function depending on the actual type of the object on which the method is called
Page: 587
Chapter: 20
Section: 3
Context: C++

Name: run-time polymorphic type
Definition: A type having virtual function
Page: 587, 760
Chapter: 20, 760
Section: 3
Context: C++
Intuition: polymorphism is determined at run time depending on type. As opposite to template which is determined at compile time

Name: The virtual table of C
Definition: A map fom virtual methods of C to implementations of those methods
Page: 588
Chapter: 20
Section: 3
Variables: C a run-timpe polymorphic type
Context: C++
Abbreviation: vtbl

Question: How is a call to a virtual method implemented
Answer: The software query the virtual table from the object, then the method from the virtual table, and call it
Page: 588
Chapter: 20
Section: 3
Variables: C a run-time polymorphic type
Context: C++

Instruction: ClassName::Method
Page: 588
Language: C++
Chapter: 20
Section: 3
Returns: the method, as implemented in class name
Variables: Method is virtual in class ClassName
Name: Explicit qualification

Instruction: type functionName(args) = 0;
Page: 589
Meaning: functionName must be virtual and overridden in instantiated descendant
Language: C++
Chapter: 20
Section: 3.4
Name: A pure virtual function

Name: A contextual keyword
Definition: A word that is a keyword only in specific context
Page: 593
Chapter: 20
Section: 3.4
Context: C++
Example: final, override
Intuition: It was not originally a keyword and introducing it as a keyword would break existing code

Instruction: using Base::method
Page: 594
Meaning: method is considered to be directly defined in Derived too. This change the overloading rules
Language: C++
Chapter: 20
Section: 3.4
Context: in derived declaration
Variables: Base
Derived : Base

Instruction: using Base::Base
Page: 594
Meaning: Derived has a constructor which is the same as Base
Language: C++
Chapter: 20
Section: 3.4
Context: in derived declaration
Variables: Base
Derived : Base

Name: The covariant return rule
Definition: In a base class, if a virtual method return type is Base* or Base&, its overridden method can return Derived* or Derived& respectively
Page: 596
Chapter: 20
Section: 3.4

Name: An abstract class
Definition: A class using =0 in a method definition
Page: 596
Chapter: 20
Section: 3.4

Name: A virtual constructor
Definition: A virtual function that return a new object (reference/pointer) it constructed
Page: 596
Chapter: 20
Section: 3.4

Instruction: =0
Page: 598
Language: c++
Chapter: 20
Section: 4
Context: After a method declaration
Name: The virtual initializer

Name: A nonintrusive list
Definition: A list that does not require modification to its elements
Page: 600
Chapter: 20
Section: 4
Context: After a method declaration

Instruction: protected:
Page: 602
Meaning: what is declared after can be used by the code of the class, the class inheriting it and friend class
Language: C++
Chapter: 20
Section: 4
Context: After a method declaration

Instruction: p->*p_m(args)
Page: 602
Effect: similar to p->m(args)
Language: C++
Chapter: 20
Section: 4
Variables: C a class
m a member of C
p_m a pointer to m
p a pointer to an instance of C

Instruction: c.>*p_m
Page: 602
Effect: similar to c.m(args)
Language: C++
Chapter: 20
Section: 4
Variables: C a class
m a member of C
p_m a pointer to m
c an instance of C

Typ: C::*
Meaning: Pointer of a member of type C
Page: 602
Language: C++
Chapter: 20
Section: 4
Variables: C a class
m a member of C
c an instance of C

Name: Contravariance
Definition: You can assign a variable of type *Base or &Base to a value of type *Derived or &Derived respectively
Page: 610
Chapter: 20
Section: 4
Context: C++

Name: The empty base optimization
Definition: if a base class has no non- static data members, no
memory will be allocated for it in an object of derived class
Page: 770
Chapter: 27
Section: 4.1
Context: C++