Created
March 10, 2019 08:46
-
-
Save fullmated/bb51ad85f43a9dedfac13dbbfd906867 to your computer and use it in GitHub Desktop.
Effective C++ #9 after
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
#include <iostream> | |
#include <memory> | |
class Info { | |
public: | |
Info(int id, std::string name){ this->id = id; this->name = name; } | |
int id; | |
std::string name; | |
}; | |
class Base { | |
public: | |
explicit Base(const Info& userInfo) { init(userInfo); } | |
void log(const Info& userInfo) const { | |
std::cout << userInfo.id << ", " << userInfo.name << std::endl; | |
} | |
private: | |
void init(const Info& userInfo) { | |
std::cout << "do something" << std::endl; | |
log(userInfo); | |
} | |
}; | |
class Derived : public Base { | |
public: | |
Derived(int id, std::string name) : Base(*createInfo(id, name)) {}; | |
private: | |
static std::shared_ptr<Info> createInfo(int id, std::string name) { | |
return std::make_shared<Info>(id, name); | |
} | |
}; | |
int main(void){ | |
std::shared_ptr<Derived> ptr = std::make_shared<Derived>(123, "Honda"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment