CopyPastor

Detecting plagiarism made easy.

Score: 1.843400776386261; Reported for: String similarity, Exact paragraph match Open both answers

Possible Plagiarism

Reposted on 2024-10-30
by Golden Rockefeller

Original Post

Original - Posted on 2024-10-30
by Golden Rockefeller



            
Present in both answers; Present only in the new answer; Present only in the old answer;

Here is my take on this question:
You can get closer to truly covariant smart pointers return types by creating an automated implementation class that reroutes pointers automatically. This way also works well with custom deleters, and this would also work when the derived class doesn't have sole shared ownership of the pointer they are returning. The code would look like this:
class Base{ virtual void print_impl(std::shared_ptr<Base>& base_ptr) = 0; public: std::shared_ptr<Base> print() { std::shared_ptr<Base> base; print_impl(base); return base; } }; template <typename TBase> class BaseImpl : public Base{ void print_impl(std::shared_ptr<Base>& base_ptr) override { auto tbase = create(); base_ptr = tbase; } public: virtual std::shared_ptr<TBase> print() = 0; }; class Derived : public BaseImpl<Derived> { public: std::shared_ptr<Derived> print(); };
In the above example, all derived classes only have to implement std::shared_ptr<Derived> print(); and inherit from BaseImpl<Derived>, where "Derived" can be compatible type.
For an executable example:
#include <iostream> #include <memory> class Base { public: virtual ~Base() {std::cout << "Destroyed\n";} virtual void print() { std::cout << "Base\n"; } }; class Derived : public Base { public: void print() override { std::cout << "Derived\n"; } }; class IFactory{ public: std::shared_ptr<Base> create() { std::shared_ptr<Base> base; create_impl(base); return base; } private: virtual void create_impl(std::shared_ptr<Base>& base_ptr) = 0; }; template<typename TBase> class UFactory : public IFactory{ public: virtual std::shared_ptr<TBase> create() = 0; private: virtual void create_impl(std::shared_ptr<Base>& base_ptr) { auto tbase = create(); shared_ptr = tbase; } }; class Factory : public UFactory<Base>{ public: std::shared_ptr<Base> create() override { return std::make_shared<Base>(); } }; class DerivedFactory : public UFactory<Derived> { public: std::shared_ptr<Derived> create() override { return std::make_shared<Derived>(); } }; int main() { { std::shared_ptr<IFactory> factory = std::make_shared<DerivedFactory>(); std::shared_ptr<Base> base = factory->create(); std::cout << typeid(factory->create().get()).name() << std::endl; // Output: Base base->print(); // Output: Derived } std::cout << "-----------" << std::endl; { std::shared_ptr<DerivedFactory> factory = std::make_shared<DerivedFactory>(); std::shared_ptr<Base> base = factory->create(); std::cout << typeid(factory->create().get()).name() << std::endl; // Output: Derived base->print(); // Output: Derived } return 0; }
You can get closer to truly covariant smart pointers return types by creating an automated implementation class that reroutes pointers automatically. This way also works well with custom deleters, and this would also work when the derived class doesn't have sole shared ownership of the pointer they are returning. The code would look like this:
class Base{ virtual void print_impl(std::shared_ptr<Base>& base_ptr) = 0; public: std::shared_ptr<Base> print() { std::shared_ptr<Base> base; print_impl(base); return base; } }; template <typename TBase> class BaseImpl : public Base{ void print_impl(std::shared_ptr<Base>& base_ptr) override { auto tbase = create(); base_ptr = tbase; } public: virtual std::shared_ptr<TBase> print() = 0; }; class Derived : public BaseImpl<Derived> { public: std::shared_ptr<Derived> print(); };
In the above example, all derived classes only have to implement std::shared_ptr<Derived> print(); and inherit from BaseImpl<Derived>, where "Derived" can be compatible type.
For an executable example:
#include <iostream> #include <memory> class Base { public: virtual ~Base() {std::cout << "Destroyed\n";} virtual void print() { std::cout << "Base\n"; } }; class Derived : public Base { public: void print() override { std::cout << "Derived\n"; } }; class IFactory{ public: std::shared_ptr<Base> create() { std::shared_ptr<Base> base; create_impl(base); return base; } private: virtual void create_impl(std::shared_ptr<Base>& base_ptr) = 0; }; template<typename TBase> class UFactory : public IFactory{ public: virtual std::shared_ptr<TBase> create() = 0; private: virtual void create_impl(std::shared_ptr<Base>& base_ptr) { auto tbase = create(); shared_ptr = tbase; } }; class Factory : public UFactory<Base>{ public: std::shared_ptr<Base> create() override { return std::make_shared<Base>(); } }; class DerivedFactory : public UFactory<Derived> { public: std::shared_ptr<Derived> create() override { return std::make_shared<Derived>(); } }; int main() { { std::shared_ptr<IFactory> factory = std::make_shared<DerivedFactory>(); std::shared_ptr<Base> base = factory->create(); std::cout << typeid(factory->create().get()).name() << std::endl; // Output: Base base->print(); // Output: Derived } std::cout << "-----------" << std::endl; { std::shared_ptr<DerivedFactory> factory = std::make_shared<DerivedFactory>(); std::shared_ptr<Base> base = factory->create(); std::cout << typeid(factory->create().get()).name() << std::endl; // Output: Derived base->print(); // Output: Derived } return 0; }

        
Present in both answers; Present only in the new answer; Present only in the old answer;