Created
June 9, 2020 08:51
-
-
Save sbliven/359d180753febc4777ac79bb97685b5b to your computer and use it in GitHub Desktop.
Some solutions from "Declaring an object before initializing it in c++" https://stackoverflow.com/questions/800368/declaring-an-object-before-initializing-it-in-c/62247236#62247236
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Solutions to https://stackoverflow.com/questions/800368/declaring-an-object-before-initializing-it-in-c/62247236#62247236 | |
* | |
* Uses c++17 for some solutions | |
* | |
* Output: | |
* | |
* $ g++ --std=c++17 -o animals animals.cpp && ./animals | |
* Naive Method | |
* ------------ | |
* Default Constructing Animal at 0x7ffee3fc20d0 | |
* Constructing puppies at 0x7ffee3fc20f0 | |
* Deconstructing puppies at 0x7ffee3fc20f0 | |
* Deconstructing puppies at 0x7ffee3fc20d0 | |
* | |
* Opt Method | |
* ---------- | |
* Constructing toads at 0x7ffee3fc2160 | |
* optional size: 40 | |
* original size: 32 | |
* Deconstructing toads at 0x7ffee3fc2160 | |
* | |
* Direct Method | |
* ---------- | |
* Constructing Butterfly at 0x7ffee3fc2140 | |
* Deconstructing Butterfly at 0x7ffee3fc2140 | |
*/ | |
#include <string> | |
#include <iostream> | |
#include <memory> | |
#include <optional> | |
class Animal { | |
public: | |
std::string name; | |
Animal(): name("Animal") { | |
std::cout << "Default Constructing " << name << " at " << this << std::endl; | |
} | |
Animal(Animal const& a): name(a.name) { | |
std::cout << "Copy Constructing at " << this << " from " << &a << std::endl; | |
} | |
Animal(std::string name): name(name) { | |
std::cout << "Constructing " << name << " at " << this << std::endl; | |
} | |
~Animal() { | |
std::cout << "Deconstructing " << name << " at " << this << std::endl; | |
} | |
}; | |
bool _happy = false; | |
bool happyDay() { | |
_happy = !_happy; | |
return _happy; | |
} | |
void naive() { | |
Animal a; | |
if( happyDay() ) | |
a = Animal(std::string("puppies" )); //constructor call | |
else | |
a = Animal( "toads" ); | |
} | |
void opt1() { | |
std::optional<Animal> a; | |
if( happyDay() ) | |
a = Animal("puppies" ); //constructor call | |
else | |
a = Animal( "toads" ); | |
} | |
void opt() { | |
std::optional<Animal> a; | |
if( happyDay() ) | |
a.emplace("puppies" ); //constructor call | |
else | |
a.emplace( "toads" ); | |
std::cout << "optional size: " << sizeof(a) << std::endl; | |
std::cout << "original size: " << sizeof(a.value()) << std::endl; | |
} | |
void direct() { | |
Animal a("Butterfly"); | |
} | |
int main(int argc, const char* argv[]) { | |
std::cout << "Naive Method" << std::endl; | |
std::cout << "------------" << std::endl; | |
naive(); | |
std::cout << std::endl; | |
std::cout << "Opt Method" << std::endl; | |
std::cout << "----------" << std::endl; | |
opt(); | |
std::cout << std::endl; | |
std::cout << "Direct Method" << std::endl; | |
std::cout << "----------" << std::endl; | |
direct(); | |
std::cout << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment