Question

I want to keep the class simple and not defined a constructor so i can do Pt data = {0, 5}; so i figured the best way convert Pt_t from a short to long or vice versa is to do something like this.

template <class T>
struct Pt_t
{
    T x, y;
    template <class T2> operator Pt_t<T2>() { Pt_t pt = {x, y}; return pt; }
};

The compiler doesnt like this and calls operator Pt_t on return pt; thus getting a stack overflow. How do i prevent this? the only solution i can think of is having Pt_t use constructors removing Pt_t pt = {1, 2}; which i prefer to keep if i can.

Was it helpful?

Solution

I'm pretty sure the unqualified Pt_t in your functions body is Pt_t<T>, but don't you want it to be Pt_t<T2>? You'll need to explicitly qualify it.

OTHER TIPS

I'm unfamiliar with C++, but are you declaring the right type in your method there?

Shouldn't it be Pt_t<T2> instead of just Pt_t ?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top