pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

在模板派生類中,為什么我需要使用“this->&

In a templated derived class, why do I need to qualify base class member names with quot;this-gt;quot; inside a member function?(在模板派生類中,為什么我需要使用“this-來限定基類成員名稱?在成員函數中?) - IT屋
本文介紹了在模板派生類中,為什么我需要使用“this->"來限定基類成員名稱?在成員函數中?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

當我調查 Qt 的源代碼時,我看到 trolltech 人員明確使用 this 關鍵字來訪問析構函數上的字段.

While I investigate source code of Qt I saw that trolltech guys explicitly use this keyword to access a field on destructor.

inline ~QScopedPointer()
{
    T *oldD = this->d;
    Cleanup::cleanup(oldD);
    this->d = 0;
}

那么,這種用法有什么意義呢?有什么好處嗎?

So, what's the point of this usage? Are there any benefits?

對于那些投票結束這個問題的人,我懷疑這種用法是用于某些類繼承的情況

For those who vote for closing this question, I suspect that this usage is for some class inheritance cases

QScopedPointer 類定義的一部分:

template <typename T, typename Cleanup = QScopedPointerDeleter<T> >
class QScopedPointer

推薦答案

C++ 答案(一般答案)

考慮一個帶有模板基類的模板類Derived:

template <typename T>
class Base {
public:
    int d;
};

template <typename T>
class Derived : public Base<T> {
    void f () {
        this->d = 0;
    }
};

this 具有類型 Derived,該類型依賴于 T.所以 this 有一個依賴類型.所以 this->d 使 d 成為依賴名稱.依賴名稱在模板定義的上下文中作為非依賴名稱和實例化上下文進行查找.

this has type Derived<T>, a type which depends on T. So this has a dependent type. So this->d makes d a dependent name. Dependent names are looked-up in the context of the template definition as non-dependent names and in the context of instantiation.

如果沒有 this->,名稱 d 只會作為非依賴名稱被查找,而不會被找到.

Without this->, the name d would only be looked-up as a non-dependent name, and not be found.

另一種解決方案是在模板定義本身中聲明d:

Another solution is to declare d in the template definition itself:

template <typename T>
class Derived : public Base<T> {
    using Base::d;
    void f () {
        d = 0;
    }
};

Qanswer(具體答案)

d 是 成員QScopedPointer.它不是繼承的成員.this-> 在這里不是必需的.

Qanswer (specific answer)

d is a member of QScopedPointer. It isn't an inherited member. this-> is not necessary here.

OTOH,QScopedArrayPointer 是一個模板類,d 是一個模板基類的繼承成員:

OTOH, QScopedArrayPointer is a template class and d is an inherited member of a template base class:

template <typename T, typename Cleanup = QScopedPointerArrayDeleter<T> >
class QScopedArrayPointer : public QScopedPointer<T, Cleanup>

所以 this-> 是必要的 此處:

inline T &operator[](int i)
{
    return this->d[i];
}

很容易看出,將 this-> 放在任何地方更容易.

It's easy to see that it's easier to just put this-> everywhere.

我想所有 C++ 用戶都不清楚為什么在非依賴基類中查找名稱而不是在依賴基類中查找名稱:

I guess it isn't clear to all C++ users why names are looked-up in non-dependent base classes but not in dependent base classes:

class Base0 {
public:
    int nd;
};

template <typename T>
class Derived2 : 
        public Base0, // non-dependent base
        public Base<T> { // dependent base
    void f () {
        nd; // Base0::b
        d; // lookup of "d" finds nothing

        f (this); // lookup of "f" finds nothing
                  // will find "f" later
    }
};

除了標準這么說"之外還有一個原因:模板中名稱綁定的工作方式.

There is a reason beside "the standard says so": cause of way name binding in templates works.

模板可以具有后期綁定的名稱,當模板被實例化時:例如 f (this) 中的 f.在Derived2::f() 定義點,編譯器不知道變量、函數或類型名稱f.f 可以引用的一組已知實體此時是空的.這不是問題,因為編譯器知道它稍后會查找 f 作為函數名或模板函數名.

Templates can have name that are bound late, when the template is instantiated: for example f in f (this). At the point of Derived2::f() definition, there is no variable, function or type name f known by the compiler. The set of known entities that f could refer to is empty at this point. This isn't a problem because the compiler knows it will lookup f later as a function name, or a template function name.

OTOH,編譯器不知道如何處理d;它不是(被調用的)函數名.無法對非(被調用)函數名稱進行后期綁定.

OTOH, the compiler doesn't know what to do with d; it isn't a (called) function name. There is no way to do late binding on non-(called) functions names.

現在,所有這些看起來像是編譯時模板多態的基本知識.真正的問題似乎是:為什么在模板定義時 d 沒有綁定到 Base::d?

Now, all of this may seem like elementary knowledge of compile-time template polymorphism. The real question seems to be: why isn't d bound to Base<T>::d at template definition time?

真正的問題是在模板定義時沒有Base::d,因為沒有完整的類型Base那個時候:Base 被聲明了,但沒有定義! 你可能會問:那怎么辦:

The real issue is that there is no Base<T>::d at template definition time, because there is no complete type Base<T> at that time: Base<T> is declared, but not defined! You may ask: what about this:

template <typename T>
class Base {
public:
    int d;
};

看起來像是一個完整類型的定義!

it looks like the definition of a complete type!

實際上,直到實例化,它看起來更像是:

Actually, until instantiation, it looks more like:

template <typename T>
class Base;

編譯器.不能在類模板中查找名稱!但僅限于模板特化(實例化).模板是使模板特化的工廠,模板不是模板特化的集合.編譯器可以在 Base 中查找任何特定類型的 T 中的 d,但它不能在類模板Base 中查找d.在確定類型 T 之前,Base::d 仍然是抽象的 Base::d;只有當類型 T 已知時,Base::d 才開始引用 int 類型的變量.

to the compiler. A name cannot be looked-up in a class template! But only in a template specialisation (instantiation). The template is a factory to make template specialisation, a template isn't a set of template specialisation. The compiler can lookup d in Base<T> for any particular type T, but it cannot lookup d in the class template Base. Until a type T is determined, Base<T>::d remains the abstract Base<T>::d; only when type T is known, Base<T>::d start to refer to a variable of type int.

這樣做的結果是類模板 Derived2 有一個完整的基類 Base0 但是一個不完整的(前向聲明的)基類 <代碼>基礎.僅對于已知類型 T,模板類"(類模板的特化)Derived2 具有完整的基類,就像任何普通類一樣.

The consequence of this is that the class template Derived2 has a complete base class Base0 but an incomplete (forward declared) base class Base. Only for a known type T, the "template class" (specialisations of a class template) Derived2<T> has a complete base classes, just like any normal class.

你現在看到了:

template <typename T>
class Derived : public Base<T> 

實際上是一個基類規范模板(一個制作基類規范的工廠),它遵循與模板內的基類規范不同的規則.

is actually a base class specification template (a factory to make base class specifications) that follows different rules from a base class specification inside a template.

備注:讀者可能已經注意到,我在解釋的最后編了幾句.

Remark: The reader may have noticed that I have made-up a few phrases at the end of the explanation.

這是非常不同的:這里dDerived中的限定名,而Derived是依賴的,因為T 是一個模板參數.限定名稱可以是后期綁定的,即使它不是(被調用的)函數名稱.

This is very different: here d is a qualified name in Derived<T>, and Derived<T> is dependent since T is a template parameter. A qualified name can be late-bound even if it isn't a (called) function name.

另一種解決方案是:

template <typename T>
class Derived : public Base<T> {
    void f () {
        Derived::d = 0; // qualified name
    }
};

這是等價的.

如果您認為在 Derived 的定義中,將 Derived 視為已知的完整類,有時將其視為未知類有時不一致,嗯,你是對的.

If you think that inside the definition of Derived<T>, the treatment of Derived<T> as a known complete class sometimes and as an unknown class some other times in inconsistent, well, you are right.

這篇關于在模板派生類中,為什么我需要使用“this->"來限定基類成員名稱?在成員函數中?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 电动高尔夫球车|电动观光车|电动巡逻车|电动越野车厂家-绿友机械集团股份有限公司 | 工业设计,人工智能,体验式3D展示的智能技术交流服务平台-纳金网 J.S.Bach 圣巴赫_高端背景音乐系统_官网 | 课件导航网_ppt课件_课件模板_课件下载_最新课件资源分享发布平台 | 广东燎了网络科技有限公司官网-网站建设-珠海网络推广-高端营销型外贸网站建设-珠海专业h5建站公司「了了网」 | 全自动变压器变比组别测试仪-手持式直流电阻测试仪-上海来扬电气 | 【ph计】|在线ph计|工业ph计|ph计厂家|ph计价格|酸度计生产厂家_武汉吉尔德科技有限公司 | 振动传感器,检波器-威海广达勘探仪器有限公司 | 洛阳永磁工业大吊扇研发生产-工厂通风降温解决方案提供商-中实洛阳环境科技有限公司 | 搜木网 - 木业全产业链交易平台,免费搜货、低价买货! | 单级/双级旋片式真空泵厂家,2xz旋片真空泵-浙江台州求精真空泵有限公司 | 植筋胶-粘钢胶-碳纤维布-碳纤维板-环氧砂浆-加固材料生产厂家-上海巧力建筑科技有限公司 | 防爆电机_防爆电机型号_河南省南洋防爆电机有限公司 | lcd条形屏-液晶长条屏-户外广告屏-条形智能显示屏-深圳市条形智能电子有限公司 | 篷房[仓储-婚庆-展览-活动]生产厂家-江苏正德装配式帐篷有限公司 | 量子管通环-自清洗过滤器-全自动反冲洗过滤器-北京罗伦过滤技术集团有限公司 | 小区健身器材_户外健身器材_室外健身器材_公园健身路径-沧州浩然体育器材有限公司 | 智能电表|预付费ic卡水电表|nb智能无线远传载波电表-福建百悦信息科技有限公司 | 干培两用箱-细菌恒温培养箱-菲斯福仪器| 国产离子色谱仪,红外分光测油仪,自动烟尘烟气测试仪-青岛埃仑通用科技有限公司 | 成都竞价托管_抖音代运营_网站建设_成都SEM外包-成都智网创联网络科技有限公司 | 锻造液压机,粉末冶金,拉伸,坩埚成型液压机定制生产厂家-山东威力重工官方网站 | 杭州标识标牌|文化墙|展厅|导视|户内外广告|发光字|灯箱|铭阳制作公司 - 杭州标识标牌|文化墙|展厅|导视|户内外广告|发光字|灯箱|铭阳制作公司 | 变频器维修公司_plc维修_伺服驱动器维修_工控机维修 - 夫唯科技 变位机,焊接变位机,焊接变位器,小型变位机,小型焊接变位机-济南上弘机电设备有限公司 | 红立方品牌应急包/急救包加盟,小成本好项目代理_应急/消防/户外用品加盟_应急好项目加盟_新奇特项目招商 - 中红方宁(北京) 供应链有限公司 | 企典软件一站式企业管理平台,可私有、本地化部署!在线CRM客户关系管理系统|移动办公OA管理系统|HR人事管理系统|人力 | 自进式锚杆-自钻式中空注浆锚杆-洛阳恒诺锚固锚杆生产厂家 | POM塑料_PBT材料「进口」聚甲醛POM杜邦原料、加纤PBT塑料报价格找利隆塑料 | 板框压滤机-隔膜压滤机配件生产厂家-陕西华星佳洋装备制造有限公司 | MVE振动电机_MVE震动电机_MVE卧式振打电机-河南新乡德诚生产厂家 | 台湾阳明固态继电器-奥托尼克斯光电传感器-接近开关-温控器-光纤传感器-编码器一级代理商江苏用之宜电气 | 智慧食堂_食堂管理系统_食堂订餐_食堂消费系统—客易捷 | 越南专线物流_东莞国际物流_东南亚专线物流_行通物流 | 干式变压器厂_干式变压器厂家_scb11/scb13/scb10/scb14/scb18干式变压器生产厂家-山东科锐变压器有限公司 | 天长市晶耀仪表有限公司| 代理记账_公司起名核名_公司注册_工商注册-睿婕实业有限公司 | 六维力传感器_六分量力传感器_模腔压力传感器-南京数智微传感科技有限公司 | 温泉机设备|温泉小镇规划设计|碳酸泉设备 - 大连连邦温泉科技 | 专注提供国外机电设备及配件-工业控制领域一站式服务商-深圳市华联欧国际贸易有限公司 | 欧必特空气能-商用空气能热水工程,空气能热水器,超低温空气源热泵生产厂家-湖南欧必特空气能公司 | loft装修,上海嘉定酒店式公寓装修公司—曼城装饰 | 切铝机-数控切割机-型材切割机-铝型材切割机-【昆山邓氏精密机械有限公司】 |