//
archives

Archive for

Storing C++ Objects in a STL Vector Part 2

1. Introduction. 1.1 In part 1, we concluded that given a base class B, a class D derived from B, and a vector V of class B, it is not possible to store an instance of D in V. Any such instance will be constructed as an object of class B before being stored inside … Continue reading

Storing C++ Objects in a STL Vector Part 1

1. Introduction. 1.1 The STL vector is one of the most useful container classes in STL. 1.2 However, given a base class B, a class D derived from B, and a vector V of type B, it is not possible to store instances of D in V. 1.3 As will be shown later on with … Continue reading

C++ Static and Dynamic Casting – Example Use Case – Part 2

1. Introduction. 1.1 In part 1 of this series of articles, I have demonstrated the shortcomings of static casting. 1.2 I reasoned that static casting is more appropriate for up-casting from a derived class to a base class and not the other way around. 1.3 So how does one safely perform a down-cast from a … Continue reading

C++ Static and Dynamic Casting – Example Use Case – Part 1

1. Introduction. 1.1 Consider the following C++ base and derived classes : class Base { public: Base() : x(0) { } int x; void func() { x = 100; cout << “Base func()” << endl; } }; class Derived : public Base { public: Derived() : y(0) { } int y; void func() { y … Continue reading