Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Animal a;
- if( happyDay() )
- a( "puppies" ); //constructor call
- else
- a( "toads" );
- Animal a(getAppropriateString());
- Animal a(happyDay() ? "puppies" : "toads");
- char *AnimalType;
- if( happyDay() )
- AnimalType = "puppies";
- else
- AnimalType = "toads";
- Animal a(AnimalType);
- auto_ptr<Animal> p_a;
- if ( happyDay() )
- p_a.reset(new Animal( "puppies" ) );
- else
- p_a.reset(new Animal( "toads" ) );
- // do stuff with p_a-> whatever. When p_a goes out of scope, it's deleted.
- Animal& a = *p_a;
- // do stuff with a. whatever
- Animal* a;
- if( happyDay() )
- a = new Animal( "puppies" ); //constructor call
- else
- a = new Animal( "toads" );
- // ...
- delete a;
- class Animal
- {
- public:
- Animal(){}
- void Init( const std::string& type )
- {
- m_type = type;
- }
- private:
- std:string m_type;
- };
- Animal a;
- if( happyDay() )
- a.Init( "puppies" );
- else
- a.Init( "toads" );
- void body(Animal & a) {
- ...
- }
- if( happyDay() ) {
- Animal a("puppies");
- body( a );
- } else {
- Animal a("toad");
- body( a );
- }
- struct AnimalDtor {
- void *m_a;
- AnimalDtor(void *a) : m_a(a) {}
- ~AnimalDtor() { static_cast<Animal*>(m_a)->~Animal(); }
- };
- char animal_buf[sizeof(Animal)]; // still stack allocated
- if( happyDay() )
- new (animal_buf) Animal("puppies");
- else
- new (animal_buf) Animal("toad");
- AnimalDtor dtor(animal_buf); // make sure the dtor still gets called
- Animal & a(*static_cast<Animal*>(static_cast<void*>(animal_buf));
- ... // carry on
- Animal a;
- if( happyDay() )
- a = Animal( "puppies" );
- else
- a = Animal( "toads" );
- Animal a;
- if( happyDay() )
- a.init( "puppies" );
- else
- a.init( "toads" );
Add Comment
Please, Sign In to add comment