#pragma once enum AircraftType { FIGHTER_ID, FREIGHTER_ID }; class Aircraft { public: Aircraft(); explicit Aircraft( AircraftType id ); virtual ~Aircraft() { delete this; }; virtual void fly(); virtual void land(); private: Aircraft* m_craft; }; class Fighter : public Aircraft { public: virtual void fly() override; virtual void land() override; private: Fighter(); Fighter(Fighter&); Fighter& operator= (Fighter&); friend class Aircraft; }; class Freighter : public Aircraft { public: virtual void fly() override; virtual void land() override; private: Freighter(); Freighter(Freighter&); Fighter& operator= (Freighter&); friend class Aircraft; }; //============================================================================== //another version, kinda Clone class Shape { public: virtual ~Shape(){}; virtual int area() const = 0; virtual Shape* clone() const = 0; // Uses the copy constructor virtual Shape* create() const = 0; // Uses the default constructor }; class Circle : public Shape { public: virtual Circle* clone() const override // Covariant Return Types { return new Circle(*this); } virtual Circle* create() const override // Covariant Return Types { return new Circle(); } virtual int area() const override { return 42; } }; ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #include "StdAfx.h" #include #include #include #include #include //value type set diagram // ______ _______ // / X \ // / / \ \ // | lv | xvl | prv | // \ \ / / // \______X_______/ // glv rv //In C++11 expressions can be classified into two categories: values with identity and values with no identity. //In this context, identity means a name, a pointer, //or a reference that enable you to determine if two objects are the same, //to change the state of an object, or copy it. // //lvalue is an expression that has identity, in contrary rvalue doesn't have. C++03 //in C++11 lvalue - expression which has identity and is not readily moveable //xvalue class A { std::string m_someStr; int m_someInt; public: A(const std::string& someStr, int someInt ) : m_someStr( someStr ) , m_someInt( someInt ) { std::cout << "'A' constructor," << std::endl << "member someStr value: " << m_someStr << std::endl << "member someInt value: " << m_someInt << std::endl << std::endl; } A() : m_someStr("My string") , m_someInt(42) { std::cout << "'A' default constructor," << std::endl << "member someStr value: " << m_someStr << std::endl << "member someInt value: " << m_someInt << std::endl << std::endl; } int getInt() const { return m_someInt; } const std::string& getString() const { return m_someStr; } A(A&& other_a) : m_someStr( std::move( other_a.m_someStr ) ) , m_someInt( other_a.m_someInt ) { std::cout << "'A' move constructor," << std::endl << std::endl; } A(const A& other_a) : m_someInt( other_a.m_someInt ) , m_someStr( other_a.m_someStr ) { std::cout << "'A' copy constructor," << std::endl << std::endl; } ~A() { std::cout << "'A' destructor," << std::endl << "member someStr value: " << m_someStr << std::endl << "member someInt value: " << m_someInt << std::endl << std::endl; } }; //============================================================================================ A&& makeAInstance() //bad example: rvalue ref to local obj { return A(); } A&& makeAInstance2() //the same bad example: rvalue ref to local obj { A a; return std::move(a); } int&& intRValue() // bad example: rvalue ref to local POD obj { static int a(23); return std::move(a); } //============================================================================================ A globalVar = A(); A&& RValueToGlobalVar() // may exist, but bad coding style(using global variable) { return std::move(globalVar); } A&& RValueToStaticVar() // may exist { static A static_var = A(); return std::move(static_var); } A&& RValueToTempVar() // may exist { return A("lil", 23); } //============================================================================================ std::list makeRandomList(int size) { std::list data(size); std::generate(data.begin(), data.end(), std::rand); return data; } // Perfect forwarding //============================================================================================ //template void bar(int& smth) { std::cout << typeid(smth).name() << std::endl; } template void bar(const T& smth) { std::cout << typeid(smth).name() << std::endl; } template void foo(T& Object) { bar(Object); } template void foo(const T& Object) { bar(Object); } //class A //{ //public: // A(const int& a, const int& b) // { // a; // b; // } //}; // //auto ptr = boost::make_shared( 5, 6 ); template class Entity { public: T m_var; }; int main() { // A a = makeAInstance(); // A a2 = makeAInstance(); // Dangling rvalue reference example //============================================================================================ //int&& danglingRValueRef = intRValue(); //int someTmpInt = 5; //std::cout << someTmpInt << std::endl; //std::cout << danglingRValueRef << std::endl; // Holding on rvalue reference(or regular reference) to a temporary object ensures // that the temporary object isn't immediately destructed: //A&& a = A(); // Test for calling different ctors //============================================================================================ /*A&& var = RValueToGlobalVar(); std::cout << typeid(var).name() << std::endl; A var1 = RValueToStaticVar(); A&& var2 = RValueToTempVar(); std::cout << var2.getInt() << " "<< var2.getString() << std::endl;*/ //============================================================================================ std::list lst = makeRandomList(5); char* an = "adadadad"; std::cout << typeid(std::move(an)).name(); int someInt = 42; const int someConstInt = 42; foo(5); foo(someInt); foo(someConstInt); //foo(A()); return 0; } ///////////////////////////////////////////////////////////////////////////////////////////////////// #include "StdAfx.h" #include #include #include class BaseNoPolymorph {}; class DerivedNoPolymorph : public BaseNoPolymorph {}; class BasePolymorph { public: virtual ~BasePolymorph(){}; }; class DerivedPolymorph : public BasePolymorph { public: virtual ~DerivedPolymorph(){}; }; ///////////////////////////////////////////////////////////////////////////////////////////////////// struct Base { public: virtual void some_func() { std::cout << "Base some_func()"; } }; struct Derived : public Base { public: virtual void some_func() override { std::cout << "Derived some_func()"; } }; //class A //{ //public: // explicit A() // { // } // // A(A const& inst) // { // } //}; int main() { Base& obj = Derived(); std::thread thr(&Base::some_func, &obj); //call corresponding virtual method of Derived class //================================================================================================================= double pi = 3.14; int intPi = pi; // compiler narrowing warning int someInt = static_cast(pi); // no warning void* anyPtr = &someInt; // any pointer type can be assigned to void* int* intPtr = static_cast(anyPtr); // have to be used to convert void* into other pointer types BaseNoPolymorph* baseNoPolyPtr = new DerivedNoPolymorph(); DerivedNoPolymorph* derivedNoPolyPtr = static_cast(baseNoPolyPtr); //DerivedPolymorph* derivedPolPtr = static_cast(baseNoPolyPtr); // used only for compatible types conversion //BaseNoPolymorph base, *pointerBase = nullptr; //BaseNoPolymorph derived, *pointerDerived = nullptr; //pointerDerived = dynamic_cast(&base); // bad style, BaseNoPolymorph - not polymorphic type //pointerBase = dynamic_cast(&derived); // norm style, coz dynamic_cast is successful // for upcasting derived to one of its base classes. //================================================================================================================= //BasePolymorph* base = new BasePolymorph(); //BasePolymorph* derived = new DerivedPolymorph(); //DerivedPolymorph* derivedPolymorph = nullptr; //if (derivedPolymorph = dynamic_cast(base)) //{ // printf("Base to Derived"); //don't work //} //if (derivedPolymorph = dynamic_cast(derived)) //{ // printf("Base to Derived"); //work //} //BasePolymorph* po = nullptr; //BaseNoPolymorph* poi = nullptr; //poi = dynamic_cast(po); // //auto var = reinterpret_cast(38158); //int* a = (int*)38158; //int* somePointer = new int(10); //int someInt = reinterpret_cast(somePointer); //decltype ("sss") my_var ="sss"; return 0; }