5

I'm trying to write an API around the following class template:

template <class A, class B>
class MyClass {
    public:
        A foo;
        B bar;
        MyClass();
        MyClass(A in1, B in2) {
            foo = in1; bar = in2;
        }
        void operator = (MyClass<A, B> in) {
            foo = in.foo; bar = in.bar;
        }
};

As a part of this API, I would like implementers to be able to dynamically cast between different types of MyClass. In order to accomplish this, I have written the following function:

template<class A, class B, class C, class D>
MyClass<C, D> operator MyClass<C, D> (MyClass<A, B> in) {
    MyClass<C, D> out((C)in.foo, (D)in.bar);
    return out;
}

This code compiled without errors.


My Question:
Assuming that both C operator C (A) and D operator D (B) are defined for any given implementation of the above function, will this crazy looking function be sufficient to allow implementers to dynamically cast between types of MyClass, (via (MyClass<C, D>)MyClass<A, B>) or have I completely lost it?



Example Implementation:

/*
  * --- INCLUDE ABOVE CLASS DEFINITION HERE ---
*/

#include <iostream>
#include <string>

int main() {
    MyClass<int, char> foo(42, 'q');
    MyClass<double, std::string> bar;

    bar = (MyClass<double, std::string>) foo;
}
  • Can you provide a minimal compilable test program? The question is cool. – Chiel Mar 28 '16 at 13:47
  • 2
    Please use the term "dynamically cast" for the operation performed by `dynamic_cast`, to avoid confusion. There is nothing dynamic in what you have shown. Your crazy-looking function doesn't look valid. Type conversion is specified by either `Class::operator TYPE()` nonstatic member function (no arguments, no explicit return type -- conversion from Class to TYPE) or by a single-argument constructor Class::Class(TYPE) (conversion from TYPE to Class). – n. m. could be an AI Mar 28 '16 at 14:43
  • @n.m. Apologies, I hadn't realized that typecasting operators weren't considered "dynamic". Perhaps I should review typecasting terminology. –  Mar 28 '16 at 15:25
  • @n.m. I don't think I fully understand. Could you potentially provide a working alternative? –  Mar 28 '16 at 15:35
  • E.g http://ideone.com/hIYsui – n. m. could be an AI Mar 28 '16 at 16:14
  • @n.m. Interesting, thank you very much! –  Mar 28 '16 at 16:16

1 Answers1

1

Answer: YES.

But you must provide ALL VARIANTS OF C operator C (A) and D operator D (B), and you need a copy constructor like this:

class MyClass
{
public:
    A foo;
    B bar;
    MyClass(MyClass &cpy)// copy constructor
   {
       foo = cpy.foo;
       bar = cpy.bar;
   }
   MyClass(A in1, B in2)
   {
       foo = in1; bar = in2;
   }
};

Copy constructor is required in order to work:

    return out;

in MyClass(A in1, B in2) method

Ing. Gerardo Sánchez
  • 1,607
  • 15
  • 14
  • Just to clarify: by "all variants...", you mean that to convert from `MyClass` to `MyClass`, both `double operator double (int)` and `string operator string (char)` must be defined, correct? –  Mar 28 '16 at 15:32