private-inheritance

std::enable_shared_from_this; public vs private

末鹿安然 提交于 2019-11-29 13:28:49
I was playing around for a bit using the shared_ptr's and enable_shared_from_this, while I run into something I don't really understand. In my first attempt I constructed something like this: class shared_test : std::enable_shared_from_this<shared_test> { public: void print(bool recursive) { if (recursive) { shared_from_this()->print(false); } std::cout << "printing" << std::endl; } }; Please note that this class is extending std::enable_shared_from_this privately. This apparently makes a lot of difference because executing a something like this: int main() { auto t(std::make_shared<shared

When to use C++ private inheritance over composition?

ⅰ亾dé卋堺 提交于 2019-11-27 09:15:29
Can you give me a concrete example when is preferable to use private inheritance over composition? Personally, I will use composition over private inheritance, but there might be the case that using private inheritance is the best solution for a particular problem. Reading the C++ faq , gives you an example on using private inheritance, but I seems easier to use composition + strategy pattern or even public inheritance than private inheritance. private inheritance is typically used to represent "implemented-in-terms-of". The main use I have seen is for mixins using private multiple inheritance

Can I cast a derived class to a private base class, using C-style cast?

六眼飞鱼酱① 提交于 2019-11-27 05:15:01
Can I do this? class A { ... }; class B : private A { const A &foo() const { return *((const A *)this); } }; Can I take a subclass that inherits privately from a base class and cast it to a public version of its base class? Can I do this without it having virtual methods? My guess is yes, but I wanted to make sure it's safe / portable. Yes you can: §5.4/7 of the standard: ... the following static_cast and reinterpret_cast operations (optionally followed by a const_cast operation) may be performed using the cast notation of explicit type conversion, even if the base class type is not accessible

When to use C++ private inheritance over composition?

一个人想着一个人 提交于 2019-11-26 17:49:04
问题 Can you give me a concrete example when is preferable to use private inheritance over composition? Personally, I will use composition over private inheritance, but there might be the case that using private inheritance is the best solution for a particular problem. Reading the C++ faq, gives you an example on using private inheritance, but I seems easier to use composition + strategy pattern or even public inheritance than private inheritance. 回答1: private inheritance is typically used to

Can I cast a derived class to a private base class, using C-style cast?

…衆ロ難τιáo~ 提交于 2019-11-26 11:28:55
问题 Can I do this? class A { ... }; class B : private A { const A &foo() const { return *((const A *)this); } }; Can I take a subclass that inherits privately from a base class and cast it to a public version of its base class? Can I do this without it having virtual methods? My guess is yes, but I wanted to make sure it\'s safe / portable. 回答1: Yes you can: §5.4/7 of the standard: ... the following static_cast and reinterpret_cast operations (optionally followed by a const_cast operation) may be