How to solve this ?
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.
How to solve this ?
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.
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
and return that instance.Code:auto derived_instance = std::make_unique<DerivedWizardPage>(derived_wizard_page_constructor_arguments_if_needed);
For a, hopefully clear, working example see
[Wandbox]三へ( へ՞ਊ ՞)へ ハッハッ
Last edited by Frydac; 04-16-2019 at 03:41 PM.
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:
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.Originally Posted by Frydac
Last edited by laserlight; 04-16-2019 at 03:55 PM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
I just copied how they created interface from here
Interfaces in C++ (Abstract Classes)
So they also did the same mistake?