C++为什么这是必要的?

发布于 2025-01-25 16:00:37 字数 524 浏览 2 评论 0原文

使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文