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

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

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

問題描述

限時(shí)送ChatGPT賬號(hào)..

當(dāng)一個(gè)模板從另一個(gè)模板公開繼承時(shí),是不是應(yīng)該可以訪問基本的公共方法?

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?

推薦答案

這是有關(guān)依賴名稱的規(guī)則的一部分.Method1 不是 Method2 范圍內(nèi)的依賴名稱.所以編譯器不會(huì)在依賴的基類中查找它.

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.

有兩種方法可以解決這個(gè)問題:使用 this 或指定基本類型.有關(guān)此非常近期的帖子的更多詳細(xì)信息或在 C++ 常見問題解答.另請(qǐng)注意,您錯(cuò)過了 public 關(guān)鍵字和分號(hào).這是您的代碼的固定版本.

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();
}

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

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

相關(guān)文檔推薦

Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和簡(jiǎn)單指針的區(qū)別?)
Difference between const. pointer and reference?(常量之間的區(qū)別.指針和引用?)
How to access the contents of a vector from a pointer to the vector in C++?(c++ - 如何從指向向量的指針訪問向量的內(nèi)容?)
Meaning of *amp; and **amp; in C++(*amp; 的含義和**amp;在 C++ 中)
Why can#39;t I do polymorphism with normal variables?(為什么我不能對(duì)普通變量進(jìn)行多態(tài)?)
Dereferencing deleted pointers always result in an Access Violation?(取消引用已刪除的指針總是會(huì)導(dǎo)致訪問沖突?)
主站蜘蛛池模板: 成人国产精品久久 | 91福利在线观看视频 | 久久久精 | 91久久| 天天干天天爱天天爽 | 久久综合99 | 日韩淫片免费看 | 色综合久久伊人 | 成人日韩av | 国产视频精品免费 | 日韩在线小视频 | 国产精品久久久久久亚洲调教 | 国产真实精品久久二三区 | 亚洲欧洲在线观看视频 | 亚洲精品日韩一区二区电影 | 日日艹夜夜艹 | 亚洲精品视频久久 | 777777777亚洲妇女| 日日夜夜91 | 韩国av电影网 | 亚洲精品视频免费观看 | 免费视频一区二区三区在线观看 | 亚洲欧美在线一区 | 四虎影院新网址 | 精品一区二区在线观看 | 久久久久久高潮国产精品视 | 国产www. | 91国内精精品久久久久久婷婷 | 欧美亚洲另类在线 | 99国产精品99久久久久久粉嫩 | 黄毛片| 国产日韩精品视频 | 女人精96xxx免费网站p | 日韩在线一区二区三区 | 亚洲欧美日韩中文字幕一区二区三区 | 亚洲天天干| 国外激情av | 午夜a√ | 久久精品男人的天堂 | 一级毛片高清 | 日日干日日 |