简体   繁体   中英

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.

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.

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 ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM