Thread: Interface with method that return other implementation of the same Interface

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    18

    Interface with method that return other implementation of the same Interface

    How to solve this ?
    Interface with method that return other implementation of the same Interface-help-jpg

    According to my design the method should return another object of the same interface but I can't do it since it is not yet recognized by the compiler.

    Thanks.

  2. #2
    Registered User
    Join Date
    Apr 2019
    Posts
    2
    You can not create an instance of an abstract/interface class, which is why you can not return an instance.
    You can, however, return a pointer/reference to an abstract class, which is created from a an instance of a (concrete, without abstract functions) derived class. This is the basis of polymorphism, and probably what you want to do.
    You have to watch out though, when returning a pointer, it has to point to an instance that is still alive after the function has finished.
    Usually this is a (maybe const?) reference to a member of the class, and then the user is responsible for keeping the class around so the member will still exist when using the returned instance.
    Or you create a new instance on the heap inside the function and return a pointer to that instance, this instance then will outlive the scope of the function. By using new, not recommended as the user of that class/function now has to know it has to call delete at some point, or with a smart pointer like std::unique_ptr<IWizardPage>, which is what I probably would do in the case you are not returning a pointer to a member of the class.
    In a derived class you then implement this abstract function and at some point you create the std::unique_ptr preferably with a call like

    Code:
    auto derived_instance = std::make_unique<DerivedWizardPage>(derived_wizard_page_constructor_arguments_if_needed);
    and return that instance.

    For a, hopefully clear, working example see
    [Wandbox]三へ( へ՞ਊ ՞)へ ハッハッ
    Last edited by Frydac; 04-16-2019 at 03:41 PM.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    By design since it is meant to be an interface, your class is an abstract base class, therefore it is impossible to create objects of the class, hence it is impossible for ProcessPage to return an object.

    Your options:
    • Return a reference. This especially makes sense if the object to be returned already exists separately, but it sounds like the method overrides will be invoking a derived class constructor, so maybe not.
    • Return a pointer to a new object. This is likely to be what you're looking to do.
    • Return a smart pointer, in particular a std::unique_ptr<IWizardPage>, to a new object. This is the same as the previous option, except that you preempt memory leaks from the start.


    EDIT:
    Quote Originally Posted by Frydac
    For a, hopefully clear, working example see
    [Wandbox]三へ( へ՞ਊ ՞)へ ハッハッ
    Unfortunately that example doesn't declare the base class destructor to be virtual, and I notice that's missing from gil900's IWizardPage too. Without that, the unique_ptr to base class approach results in undefined behaviour since it destroys the derived class object through a base class pointer. That it is an interface with no member variables of its own reduces the problem, but it is still undefined behaviour.
    Last edited by laserlight; 04-16-2019 at 03:55 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Apr 2019
    Posts
    2
    Quote Originally Posted by laserlight View Post
    [...]

    Unfortunately that example doesn't declare the base class destructor to be virtual, and I notice that's missing from gil900's IWizardPage too. Without that, the unique_ptr to base class approach results in undefined behaviour since it destroys the derived class object through a base class pointer. That it is an interface with no member variables of its own reduces the problem, but it is still undefined behaviour.
    ooch! Indeed.

    same example with virtual destructor:
    [Wandbox]三へ( へ՞ਊ ՞)へ ハッハッ

    This illustrates you always want to use tools like gcc/clang address sanitizers, or msvc /RTC (Run-Time Error Checks, though I'm not sure if this would catch the absence of the virtual destructor) and use tools like valgrind (don't know about a windows alternative).
    Last edited by Frydac; 04-17-2019 at 06:55 AM.

  5. #5
    Registered User
    Join Date
    Apr 2019
    Posts
    18
    I just copied how they created interface from here
    Interfaces in C++ (Abstract Classes)

    So they also did the same mistake?

  6. #6
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by gil900 View Post
    I just copied how they created interface from here
    Interfaces in C++ (Abstract Classes)

    So they also did the same mistake?
    Where in the code on that page does class Shape have a method that returns an object of type Shape?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please critique my Queue interface and implementation
    By CodeSlapper in forum C Programming
    Replies: 8
    Last Post: 02-22-2016, 12:02 PM
  2. Seperating interface and implementation
    By Swerve in forum C++ Programming
    Replies: 2
    Last Post: 01-27-2010, 08:59 PM
  3. interface implementation
    By George2 in forum C# Programming
    Replies: 2
    Last Post: 05-04-2008, 12:46 AM
  4. Replies: 5
    Last Post: 06-19-2006, 03:43 PM
  5. interface, implementation, & application files
    By trongsi in forum C++ Programming
    Replies: 4
    Last Post: 04-08-2006, 02:11 PM

Tags for this Thread