Object-Oriented Programming C++ Miscellaneous: Roman Podraza
Object-Oriented Programming C++ Miscellaneous: Roman Podraza
C++ Miscellaneous
Roman Podraza
Institute of Computer Science
Warsaw University of Technology
Namespaces
Namespace is a independent context area,
where names (identifiers) are separated from
other namespaces.
The namespace identifier is (can be) used as a part of
scope resolved name
namespace Education {
class university { .... };
class high_school{ .... };
....
}
Education::university PW; // name university (of a class)
// from namespace Education
2
Namespaces
A using declaration gives access to all names
from a namespace
using namespace Education;
high_school British_College;
Namespaces
Namespace can be anonymous and can be
used instead of static global
namespace {
int index = 0; // index cannot be accessed beyond the file
}
Casting
Static casting ( static_cast<type> )
Relatively safe
Can be applied only for related types (e.g. between
pointers, enumeration and integral, floating-point and
integral)
double x, y = 2.6;
x = static_cast<double> (static_cast<int>(y) + 1);
//relatively safe
enum peer {king, prince, earl} a;
enum animal {horse, frog, snake} b;
a = static_cast<peer>(frog);
5
Casting
Reinterpreted casting ( reinterpret_cast<type> )
Between any types
System dependent and unsafe
double x;
int i = reinterpret_cast<int>(&x);
Casting
Constant casting ( const_cast<type> )
This type of casting manipulates the constness of an
object, either to be set or to be removed. For example
in order to pass a const argument to a function that expects a
non-constant parameter
or to remove attribute of being constant
Casting
class Date {
bool buffer_ok;
string buffer;
void calc_buf_value();
// ...
public:
// ...
string text_rep() const;
}
string Date::text_rep() const {
if (buffer_ok == false) {
Date* th = const_cast<Date*>(this); //remove constness
th -> calc_buf_value();
th -> buffer_ok = true;
}
return buffer;
}
//...
Date d1;
const Date d2;
string s1 = d1.text_rep();
string s2 = d2.text_rep(); // undefined behavior
Casting
Dynamic casting ( dynamic_cast<type> )
can be used only with pointers and references to
objects; its purpose is to ensure that the result of the
type conversion is a valid complete object of the
requested class
dynamic_cast<pointer_type>(pointer) works with classes
having virtual functions (requires polymorphic
argument - run-time checking is feasible)
if the conversion is inappropriate, the NULL pointer is
returned
9
Casting
class Base {
// ...
virtual void f();
// ...
};
class Derived: public Base {
// ...
};
void fcn (Base * ptr)
{
Derived *dptr = dynamic_cast<Derived*>(ptr);
// assignes NULL if ptr points on object of Base class
// ...
}
10
typeid
typeid
allows to check the type of an expression:
typeid (expression)
typeid
#include <iostream>
#include <typeinfo>
using namespace std;
class CBase { virtual void f(){} }; // polymorphic class
class CDerived : public CBase {};
int main () {
Base* a = new Base;
Base* b = new Derived;
cout << "a is: " << typeid(a).name() << '\n';
cout << "b is: " << typeid(b).name() << '\n';
cout << "*a is: " << typeid(*a).name() << '\n';
cout << "*b is: " << typeid(*b).name() << '\n';
if (typeid(*b) == typeid (Derived))
{
// ...
}
return 0;
}
12
Mutable members
mutable
The keyword mutable is used to allow a particular
data member of const object to be modified.
This is particularly useful if most of the members
should be constant but a few need to be updateable.
13
Mutable members
class person {
public:
person (const char* pname, int page);
void bday() { ++age;}
private:
const char* name;
mutable int age; // always can be modified
}
14
vector
stack
list
queue
dequeue
priority queue
15
Hashing maps
16
17
- non-modifying
operations
modifying operations
- sorted
collections
18
Thank You