Guest User

Untitled

a guest
Jun 20th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. Animal a;
  2. if( happyDay() )
  3. a( "puppies" ); //constructor call
  4. else
  5. a( "toads" );
  6.  
  7. Animal a(getAppropriateString());
  8.  
  9. Animal a(happyDay() ? "puppies" : "toads");
  10.  
  11. char *AnimalType;
  12. if( happyDay() )
  13. AnimalType = "puppies";
  14. else
  15. AnimalType = "toads";
  16. Animal a(AnimalType);
  17.  
  18. auto_ptr<Animal> p_a;
  19. if ( happyDay() )
  20. p_a.reset(new Animal( "puppies" ) );
  21. else
  22. p_a.reset(new Animal( "toads" ) );
  23.  
  24. // do stuff with p_a-> whatever. When p_a goes out of scope, it's deleted.
  25.  
  26. Animal& a = *p_a;
  27.  
  28. // do stuff with a. whatever
  29.  
  30. Animal* a;
  31. if( happyDay() )
  32. a = new Animal( "puppies" ); //constructor call
  33. else
  34. a = new Animal( "toads" );
  35.  
  36. // ...
  37. delete a;
  38.  
  39. class Animal
  40. {
  41. public:
  42. Animal(){}
  43. void Init( const std::string& type )
  44. {
  45. m_type = type;
  46. }
  47. private:
  48. std:string m_type;
  49. };
  50.  
  51. Animal a;
  52. if( happyDay() )
  53. a.Init( "puppies" );
  54. else
  55. a.Init( "toads" );
  56.  
  57. void body(Animal & a) {
  58. ...
  59. }
  60.  
  61. if( happyDay() ) {
  62. Animal a("puppies");
  63. body( a );
  64. } else {
  65. Animal a("toad");
  66. body( a );
  67. }
  68.  
  69. struct AnimalDtor {
  70. void *m_a;
  71. AnimalDtor(void *a) : m_a(a) {}
  72. ~AnimalDtor() { static_cast<Animal*>(m_a)->~Animal(); }
  73. };
  74.  
  75. char animal_buf[sizeof(Animal)]; // still stack allocated
  76.  
  77. if( happyDay() )
  78. new (animal_buf) Animal("puppies");
  79. else
  80. new (animal_buf) Animal("toad");
  81.  
  82. AnimalDtor dtor(animal_buf); // make sure the dtor still gets called
  83.  
  84. Animal & a(*static_cast<Animal*>(static_cast<void*>(animal_buf));
  85. ... // carry on
  86.  
  87. Animal a;
  88. if( happyDay() )
  89. a = Animal( "puppies" );
  90. else
  91. a = Animal( "toads" );
  92.  
  93. Animal a;
  94. if( happyDay() )
  95. a.init( "puppies" );
  96. else
  97. a.init( "toads" );
Add Comment
Please, Sign In to add comment