简体   繁体   English

运算符ThisClass()导致堆栈溢出

[英]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}; 我想保持类的简单,而不是定义一个构造函数,所以我可以做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. 所以我想最好的方法是将Pt_t从短转换为长,反之亦然。

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; 编译器不喜欢这样,并在返回pt时调用运算符Pt_t。 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}; 我能想到的唯一解决方案是让Pt_t使用构造函数删除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> ? 我很确定函数主体中不合格的Pt_t是Pt_t<T> ,但是您不希望它是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? 我不熟悉C ++,但是您是否在那里声明了正确的方法类型?

Shouldn't it be Pt_t<T2> instead of just Pt_t ? 它不是Pt_t<T2>而不是Pt_t吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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