Skip to content

Instantly share code, notes, and snippets.

@fullmated
Created March 10, 2019 08:46
Show Gist options
  • Save fullmated/bb51ad85f43a9dedfac13dbbfd906867 to your computer and use it in GitHub Desktop.
Save fullmated/bb51ad85f43a9dedfac13dbbfd906867 to your computer and use it in GitHub Desktop.
Effective C++ #9 after
#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