Paste #130773 |
pasted on 30.12.2019 00:50
- Edit to this paste
- Raw
-
Compare with paste
#
Text paste
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 | #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 <Windows.h> #include <string> #include <iostream> #include <list> #include <algorithm> //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<int> makeRandomList(int size) { std::list<int> data(size); std::generate(data.begin(), data.end(), std::rand); return data; } // Perfect forwarding //============================================================================================ //template <typename T> void bar(int& smth) { std::cout << typeid(smth).name() << std::endl; } template <typename T> void bar(const T& smth) { std::cout << typeid(smth).name() << std::endl; } template <typename T> void foo(T& Object) { bar(Object); } template <typename T> void foo(const T& Object) { bar(Object); } //class A //{ //public: // A(const int& a, const int& b) // { // a; // b; // } //}; // //auto ptr = boost::make_shared<A>( 5, 6 ); template <typename T> 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<int> 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 <stdio.h> #include <iostream> #include <thread> 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<int>(pi); // no warning void* anyPtr = &someInt; // any pointer type can be assigned to void* int* intPtr = static_cast<int*>(anyPtr); // have to be used to convert void* into other pointer types BaseNoPolymorph* baseNoPolyPtr = new DerivedNoPolymorph(); DerivedNoPolymorph* derivedNoPolyPtr = static_cast<DerivedNoPolymorph*>(baseNoPolyPtr); //DerivedPolymorph* derivedPolPtr = static_cast<DerivedPolymorph*>(baseNoPolyPtr); // used only for compatible types conversion //BaseNoPolymorph base, *pointerBase = nullptr; //BaseNoPolymorph derived, *pointerDerived = nullptr; //pointerDerived = dynamic_cast<DeriveNoPolymorph*>(&base); // bad style, BaseNoPolymorph - not polymorphic type //pointerBase = dynamic_cast<BaseNoPolymorph*>(&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<DerivedPolymorph*>(base)) //{ // printf("Base to Derived"); //don't work //} //if (derivedPolymorph = dynamic_cast<DerivedPolymorph*>(derived)) //{ // printf("Base to Derived"); //work //} //BasePolymorph* po = nullptr; //BaseNoPolymorph* poi = nullptr; //poi = dynamic_cast<BaseNoPolymorph*>(po); // //auto var = reinterpret_cast<int*>(38158); //int* a = (int*)38158; //int* somePointer = new int(10); //int someInt = reinterpret_cast<int>(somePointer); //decltype ("sss") my_var ="sss"; return 0; } |