user3856948
user3856948

Reputation:

C++ two class convert each other and call their functions

I've two classes.One of them has no return value and throwing values,other one has return type and doesn't throw anything.What i want to achieve is convert classes between each other and call their functions,i can't use static method for calling functions.Example of code block;

  class ThrowableA{
        public:
            void getvalue();
    }

    class ReturnTypeA{
        public:
            int getvalue();
    }


    class BaseTypeA{//this will accept ThrowableA and ReturnTypeA
    ...
    };

    BaseTypeA * d = new ThrowableA();
    d->getvalue(); // this will call ThrowableA::getvalue
    (do something with d)->getvalue();//this will call ReturnTypeA::getvalue
    BaseTypeA * f = new ReturnTypeA();
    f->getvalue();//this will call ReturnTypeA::getvalue
    (do something with f)->getvalue();//this will call ThrowableA::getvalue

Upvotes: 1

Views: 1064

Answers (2)

NicholasM
NicholasM

Reputation: 4673

I think you need to define copy constructors (or move constructors); alternatively, you can define conversion operators:

class ThrowableA : public BaseTypeA
{
    public:
        explicit ThrowableA(const ReturnTypeA& rt);
        void getvalue();
        // or explicit operator ReturnTypeA() const;
}

class ReturnTypeA : public BaseTypeA
{
    public:
        explicit ReturnTypeA(const ThrowableA& ta);
        int getvalue();
        // or explicit operator ThrowableA() const;
}

The explicit keyword means the conversion will only take place when your code specifically requests it. Without that keyword, the language will automatically perform up to one conversion where necessary, which can lead to surprises. Bjarne Stroustrup's FAQ mentions explicit conversion operators; it's apparently a C++11 feature (while the explicit constructors are from C++03).

The inheritance is not required for conversion back and forth, but seems to match the setup you want.

Then, to convert from one to another, simply create another object, of the other type:

ThrowableA ta;
// Work with ta, add data, etc.
ReturnTypeA ra(ta);
// Use ra.

Upvotes: 1

Zorgiev
Zorgiev

Reputation: 814

You need to redefine your code this way:

class BaseTypeA;
class ReturnTypeA;

class ThrowableA : public BaseTypeA{
    public:
        explicit ThrowableA(ReturnTypeA const & rhs);
        explicit operator (ReturnTypeA const)(); // explicit conversion operators is a C++11 feature
        void getvalue();
};

class ReturnTypeA : public BaseTypeA{
    public:
        explicit ReturnTypeA(ThrowableA const & rhs);
        explicit operator (ThrowableA const)();     
        int getvalue();
};


class BaseTypeA{//this will accept ThrowableA and ReturnTypeA
...
};

BaseTypeA * d = new ThrowableA();
dynamic_cast<ThrowableA *>(d)->getvalue(); // getvalue isn't in scope of base class.
//(do something with d)->getvalue();//this will call ReturnTypeA::getvalue
BaseTypeA * f = new ReturnTypeA();
dynamic_cast<ReturnTypeA *>(f)->getvalue();//getvalue isn't in scope of base class.
//(do something with f)->getvalue();//this will call ThrowableA::get value

You can consider moving getvalue to the base class and making it pure virtual, but their return types differ so that can't be possible - either make return types the same, or no movement to base class scope.

Upvotes: 0

Related Questions