C++为什么这是必要的?
使用GCC,我们无法明确编写明确地访问基类中的成员,但是它在MSVC上起作用,这是怎么回事?是因为CRTP吗?
#include <iostream>
template<class T>
struct Base {
protected:
T* a;
};
template<class U>
struct Derived : Base<Derived<U>> {
void print_a() {
std::cout << a << std::endl; // doesn't work on GCC
std::cout << this->a << std::endl; // works on GCC
}
};
int main() {
Derived<float> d;
d.print_a();
}
With GCC, we can't access the member in the base class without writing this
explicitly, but it works on MSVC, what is going on? Is it because of the CRTP?
#include <iostream>
template<class T>
struct Base {
protected:
T* a;
};
template<class U>
struct Derived : Base<Derived<U>> {
void print_a() {
std::cout << a << std::endl; // doesn't work on GCC
std::cout << this->a << std::endl; // works on GCC
}
};
int main() {
Derived<float> d;
d.print_a();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论