4-C - Advanced

Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

www.kernkonzept.

com

Advanced C++ Topics


Alexander Warg, 2018

MICROKERNEL MADE IN GERMANY


Overview

WHAT IS BEHIND C++



Language Magics

Object Life Time

Object Memory Layout

INTRODUCTION TO TEMPLATES

Template Function

Template Class

WHAT I DO NOT EXPLAIN



Standard C++ Library
C++ vs C

Some more keywords


– new, delete, class, virtual, mutable, explicit...
Stricter type system
– e.g. no automatic conversion from void *
– custom class types
– strictly typed enums (C++11)
Function overloading / Operator overloading
– multiple functions with the same name but
different arguments
Extensible hierarchical type system
– classes and inheritance
Generic programming via templates
CONSTRUCTION AND DESTRUCTION

Constructors: Special Member Functions for object initialization


– Same name as the class
– No return type
CONSTRUCTION AND DESTRUCTION

Constructors: Special Member Functions for object initialization


– Same name as the class
– No return type

Destructors: Special Member Functions for object destruction


– Name: ~Classname()
– No return type
– No arguments
CONSTRUCTORS (CLASS FOO)
CONSTRUCTORS (CLASS FOO)

Foo() -> Default Constructor


No arguments
Generated by Compiler if no other Constructors
CONSTRUCTORS (CLASS FOO)

Foo() -> Default Constructor


No arguments
Generated by Compiler if no other Constructors

Foo(Type x) -> Conversion Constructor


Is used to cast type Type to Foo (implicitly)
(see keyword explicit)
CONSTRUCTORS (CLASS FOO)

Foo() -> Default Constructor


No arguments
Generated by Compiler if no other Constructors

Foo(Type x) -> Conversion Constructor


Is used to cast type Type to Foo (implicitly)
(see keyword explicit)

Foo(Foo const &o) -> Copy Constructor


Always generated by Compiler if not provided
(related to operator = (Foo const &o), see later)
CONSTRUCTORS (CLASS FOO)

Foo() -> Default Constructor


No arguments
Generated by Compiler if no other Constructors

Foo(Type x) -> Conversion Constructor


Is used to cast type Type to Foo (implicitly)
(see keyword explicit)

Foo(Foo const &o) -> Copy Constructor


Always generated by Compiler if not provided
(related to operator = (Foo const &o), see later)

Foo(Type a, Type b, Type c) -> Normal Constructor


CONSTRUCTORS (CLASS FOO) C++11

Foo(Foo &&o) -> Move Constructor


(related to operator = (Foo &&o), see later)
CONSTRUCTORS (CLASS FOO) C++11

Foo(Foo &&o) -> Move Constructor


(related to operator = (Foo &&o), see later)

Type &&x -> RValue refernce


Fast optimized move operations
Support for perfect forwarding
TYPE CONVERSION / CAST

Implicit type conversion


– among integer types (incl. enum)
– conversion ctor ?
– conversion operator ?
– from pointers/references of derived classes to
pointers/references to base classes
Explicit type conversion (casts)
C++ has three (actually four) types of casts
– static_cast<type>(...)
– reinterpret_cast<type>(...)
– dynamic_cast<type>(...)
– const_cast<type>(...)
POLYMORPHISM (ADVANCED)

Virtual Functions
Support for Overriding functions in C++
Pure Virtual Functions (Abstract Function)
class A { void func() = 0; };
<A> cannot be instantiated (is abstract)
Multiple Inheritance
class A : public B, public C {...};
POLYMORPHISM – VIRTUAL DTOR

Virtual deletion ...


POLYMORPHISM II

MULTIPLE INHERITANCE
POLYMORPHISM II

MULTIPLE INHERITANCE

Car
name()
POLYMORPHISM II

MULTIPLE INHERITANCE

Car Boat
name() name()
POLYMORPHISM II

MULTIPLE INHERITANCE

Object
cnt

Car Boat
name() name()
POLYMORPHISM II

MULTIPLE INHERITANCE

Object
cnt

Car Boat
name() name()

Amphi
POLYMORPHISM II

MULTIPLE INHERITANCE

Amphi

Car Boat
Object Object
POLYMORPHISM II

MULTIPLE INHERITANCE

Amphi Amphi

Car Boat Car Boat


Object Object Object
TEMPLATES – FUNCTION TEMPLATES

Functions that operate on a Generic Type (e.g. T)


TEMPLATES – FUNCTION TEMPLATES

Functions that operate on a Generic Type (e.g. T)

int max(int a, int b)


{ return a>b?a:b; }
TEMPLATES – FUNCTION TEMPLATES

Functions that operate on a Generic Type (e.g. T)

int max(int a, int b)


{ return a>b?a:b; }
int a, b;
int x = max(a, b);
TEMPLATES – FUNCTION TEMPLATES

Functions that operate on a Generic Type (e.g. T)

int max(int a, int b)


{ return a>b?a:b; }
int a, b;
int x = max(a, b);
double a, b;
double x = max(a, b);
TEMPLATES – FUNCTION TEMPLATES

Functions that operate on a Generic Type (e.g. T)

template< typename T >


T max(T a, T b)
{ return a>b?a:b; }
int a, b;
int x = max<int>(a,
max(a, b); b);
double a, b;
double x = max<double>(a,
max(a, b); b);
TEMPLATES – FUNCTION TEMPLATES

Functions that operate on a Generic Type (e.g. T)

template< typename T >


T max(T a, T b)
{ return a>b?a:b; }
int a, b;
int x = max(a, b);
double a, b;
double x = max(a, b);
TEMPLATES – CLASS TEMPLATES

Classes with members of Generic Types (e.g. T)


TEMPLATES – CLASS TEMPLATES

Classes with members of Generic Types (e.g. T)

class List_item
{
List_item *_next, *_prev;
void *_data;
};
TEMPLATES – CLASS TEMPLATES

Classes with members of Generic Types (e.g. T)

template< typename T >


class List_item
{
List_item *_next, *_prev;
void
T *_data;
*_data;
};
DON'T DO THIS...

Too Much operator overloading


Keep usual semantics
Avoid implicit conversion operators
using namespace <X> in Header Files
#define ...
Use enum's for constant values
Use templates for functions

You might also like