user34537
user34537

Reputation:

operator ThisClass() causing stack overflow

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.

Upvotes: 1

Views: 384

Answers (2)

eduffy
eduffy

Reputation: 40232

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.

Upvotes: 5

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391506

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 ?

Upvotes: 1

Related Questions