久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

C++ 中的繼承和模板——為什么繼承的成員不可見

Inheritance and templates in C++ - why are inherited members invisible?(C++ 中的繼承和模板——為什么繼承的成員不可見?)
本文介紹了C++ 中的繼承和模板——為什么繼承的成員不可見?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

當一個模板從另一個模板公開繼承時,是不是應該可以訪問基本的公共方法?

When a template publicly inherits from another template, aren't the base public methods supposed to be accessible?

template <int a>
class Test {
public:
    Test() {}
    int MyMethod1() { return a; }
};

template <int b>
class Another : public Test<b>
{
public:
    Another() {}
    void MyMethod2() {
        MyMethod1();
    }
};

int main()
{
    Another<5> a;
    a.MyMethod1();
    a.MyMethod2();
}

好吧,GCC 在這件事上胡說八道……我一定遺漏了一些非常明顯的東西(大腦融化).有幫助嗎?

Well, GCC craps out on this... I must be missing something totally obvious (brain melt). Help?

推薦答案

這是有關依賴名稱的規則的一部分.Method1 不是 Method2 范圍內的依賴名稱.所以編譯器不會在依賴的基類中查找它.

This is part of the rules concerning dependent names. Method1 is not a dependent name in the scope of Method2. So the compiler doesn't look it up in dependent base classes.

有兩種方法可以解決這個問題:使用 this 或指定基本類型.有關此非常近期的帖子的更多詳細信息或在 C++ 常見問題解答.另請注意,您錯過了 public 關鍵字和分號.這是您的代碼的固定版本.

There two ways to fix that: Using this or specifying the base type. More details on this very recent post or at the C++ FAQ. Also notice that you missed the public keyword and a semi-colon. Here's a fixed version of your code.


template <int a>
class Test {
public:
    Test() {}
    int MyMethod1() { return a; }
};

template <int b>
class Another : public Test<b>
{
public:
    Another() {}
    void MyMethod2() {
        Test<b>::MyMethod1();
    }
};

int main()
{
    Another<5> a;
    a.MyMethod1();
    a.MyMethod2();
}

這篇關于C++ 中的繼承和模板——為什么繼承的成員不可見?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和簡單指針的區別?)
Difference between const. pointer and reference?(常量之間的區別.指針和引用?)
How to access the contents of a vector from a pointer to the vector in C++?(c++ - 如何從指向向量的指針訪問向量的內容?)
Meaning of *amp; and **amp; in C++(*amp; 的含義和**amp;在 C++ 中)
Why can#39;t I do polymorphism with normal variables?(為什么我不能對普通變量進行多態?)
Dereferencing deleted pointers always result in an Access Violation?(取消引用已刪除的指針總是會導致訪問沖突?)
主站蜘蛛池模板: 国产欧美精品区一区二区三区 | 中文在线播放 | 国产精品美女久久久久aⅴ国产馆 | 久久99久久| 日韩不卡在线观看 | 国外成人在线视频网站 | 午夜三级网站 | 精品国产成人 | 国产欧美精品一区二区色综合 | 伊人久久精品一区二区三区 | 天堂综合网久久 | 欧美亚洲另类在线 | 亚洲国产一区二区三区, | 国产亚洲精品美女久久久久久久久久 | 亚洲成人999 | 色综合色综合网色综合 | 亚洲国产精品一区二区www | 99精品电影 | 亚洲精品一区在线 | 日韩欧美视频网站 | 中文字幕成人在线 | 成人一区二区在线 | www久久 | 中文字幕 国产精品 | 在线免费观看黄a | 欧美午夜精品久久久久久浪潮 | 日韩中文字幕在线视频 | 黄色大片免费网站 | 精品国产18久久久久久二百 | 久久久久国产精品 | 欧美激情国产精品 | 日韩成人在线视频 | www亚洲一区| 午夜视频在线免费观看 | 婷婷久久网 | 午夜在线视频 | 福利精品在线观看 | 亚洲一区在线日韩在线深爱 | 欧美高清一区 | 国产女人第一次做爰毛片 | 中文字幕在线观看www |