gpt4 book ai didi

c++ - 当我将类作为模板时,为什么会编译?

转载 作者:太空狗 更新时间:2023-10-29 20:42:16 25 4
gpt4 key购买 nike

我故意不正确地使用点和箭头运算符,但我很困惑为什么当我决定将类设为模板时它会编译。

编译:

template <class B> 
struct Boss {

bool operator==( Boss & other ) {

return this.x == other -> x;

}

};

int main() {

}

不编译:

struct Boss {

bool operator==( Boss & other ) {

return this.x == other -> x;

}

};

int main() {

}

最佳答案

如果未实例化模板,则不会对其正确性进行全面检查。它们只检查语法。 this.x,虽然在语义上不正确(因为 this 不是,也不可能是支持该操作的类型),但在语法上仍然是正确的。

关于c++ - 当我将类作为模板时,为什么会编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19265455/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com