Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

c++11 is it possible to create an interface with a function that allows various return types T or const T&?

I want to create an interface for classes that return a message.
My problem is that sometimes it will be better to return a string and sometimes a const string&.
What would be the best way to give inheriting classes the possibility of choosing the return type?

struct Verbose{
   // my message is a class member
   virtual const string& getMsg()const=0;

   // my message is created in the function
   virtual string getMsg()const=0;
};

Instead of this interface, I’d like something like this,
so as to offer this flexibility

struct Verbose{
   virtual myPrintableType getMsg()const=0;
};

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use ref-qualifiers for your member functions:

struct Verbose{
   virtual const std::string& getMsg() const& = 0;
   virtual std::string getMsg() const&& = 0;
};

struct Derived: Verbose {
    std::string s{"xxx"};
    const std::string& getMsg()const& {
        std::cout << "const string&\n";
        return s;
    }
    std::string getMsg()const&& {
        std::cout << "string\n";
        return "yyy";
    }
};

First version will be called only if your object is an l-value, i.e. it’s considered safe to return member field by const reference. Second version will be called for r-values.

    Derived d;
    std::cout << d.getMsg() << std::endl;  // calls 1st overload
    std::cout << Derived{}.getMsg() << std::endl;  // calls 2nd overload

This is assuming that you want to have different behavior depending on value category. If it’s not the case and you need different return types for the same value category, then you need 2 different functions.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading