portected access:模板派生与类派生 [英] portected access: template derivation vs. class derivation

查看:68
本文介绍了portected access:模板派生与类派生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,如果函数da是

取消注释,则B类将无法编译。我使用完全相同的代码,并将其转换为模板。

从未在

派生类中的基类名称中使用模板参数,代码编译时没有任何关于受保护的投诉

访问权限。为什么?模板化代码有什么不同?


A类{

公开:

A(const int& x_ = 0) :x(x _){}

受保护:

int x;

};


B级:公共A {

public:

B(const int& x_ = 0):A(x _){}

/ *

B& da(const int& da_)

{

this-> a.x + = da_; //错误:受保护的访问

返回* this;

}

* /

受保护:

A a;

};


/ *与上述相同,但有模板* /

模板< typename T>

class AT {

public:

AT(const int& x_ = 0):x(x _){}

受保护:

int x;

};


模板< typename T>

class BT:public AT< T> {

public:

BT(const int& x_ = 0):AT< T>(x _){}


BT& da(const int& da_)

{

this-> a.x + = da_; //工作正常

返回*这个;

}


受保护:

AT< T> ; a;

};


-

STH

哈顿定律:" ;只有一个不可侵犯的法律

KDevelop: http:// www。 kdevelop.org SuSE: http://www.suse.com

Mozilla: http://www.mozilla.org

In the following code, the class B will not compile if the function da is
uncommented. I took exactly the same code, and turned it into templates.
Never used the template parameter in anything but the baseclass name in the
derived class, and the code compiles without any complaints about protected
access. Why? What is different about the templated code?

class A{
public:
A(const int& x_=0): x(x_){}
protected:
int x;
};

class B: public A{
public:
B(const int& x_=0): A(x_){}
/*
B& da(const int& da_)
{
this->a.x += da_; // error: protected access
return *this;
}
*/
protected:
A a;
};

/*Same as above but with templates*/
template<typename T>
class AT{
public:
AT(const int& x_=0): x(x_){}
protected:
int x;
};

template<typename T>
class BT: public AT<T>{
public:
BT(const int& x_=0): AT<T>(x_){}

BT& da(const int& da_)
{
this->a.x += da_; // works fine
return *this;
}

protected:
AT<T> a;
};

--
STH
Hatton''s Law: "There is only One inviolable Law"
KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com
Mozilla: http://www.mozilla.org

推荐答案

Steven T. Hatton写道:
Steven T. Hatton wrote:
在下面的代码中,如果函数da是取消注释。我使用完全相同的代码,并将其转换为模板。
从未在
派生类中的基类名称中使用模板参数,并且代码编译时没有任何关于受保护的抱怨
访问。为什么?模板化代码有什么不同?


模板化代码实际上并未编译。除非你试图实现模板的
。你是否?剩下的代码在哪里?

还有什么吗?如果没有,那么所有编译器都为你的模板做了
检查语法。姓名未解决,

访问权限未被检查等。

A类{
公开:
A(const int& x_ = 0):x(x _){}
受保护:
int x;
};

B类:public A {
public:
B(const int& x_ = 0):A(x _){}
/ *
B& da(const int& da_)
{
this-> a.x + = da_; //错误:受保护访问
返回*此;
}
* /
受保护:
A a;
};

/ *与上面相同,但有模板* /
模板< typename T>
类AT {
公开:
AT(const int& x_ = 0):x( x _){}
受保护:
int x;
};

模板< typename T>
类BT:public AT< T> {<公开:
BT(const int& x_ = 0):AT< T>(x _){}

BT& da(const int& da_)
{
this-> a.x + = da_; //工作正常
返回*这个;
}

受保护:
AT< T> a;
};
In the following code, the class B will not compile if the function da is
uncommented. I took exactly the same code, and turned it into templates.
Never used the template parameter in anything but the baseclass name in the
derived class, and the code compiles without any complaints about protected
access. Why? What is different about the templated code?
The templated code is not really "compiled" unless you attempt to
instantiate the template. Did you? Where is the rest of the code?
Was there anything else? If there wasn''t, then all the compiler did
for your templates was checking the syntax. Names were not resolved,
access rights weren''t checked, etc.

class A{
public:
A(const int& x_=0): x(x_){}
protected:
int x;
};

class B: public A{
public:
B(const int& x_=0): A(x_){}
/*
B& da(const int& da_)
{
this->a.x += da_; // error: protected access
return *this;
}
*/
protected:
A a;
};

/*Same as above but with templates*/
template<typename T>
class AT{
public:
AT(const int& x_=0): x(x_){}
protected:
int x;
};

template<typename T>
class BT: public AT<T>{
public:
BT(const int& x_=0): AT<T>(x_){}

BT& da(const int& da_)
{
this->a.x += da_; // works fine
return *this;
}

protected:
AT<T> a;
};




V



V




" Steven T. Hatton <苏****** @ setidava.kushan.aa>在消息中写道

新闻:uI ******************** @ speakeasy.net ...

"Steven T. Hatton" <su******@setidava.kushan.aa> wrote in message
news:uI********************@speakeasy.net...
In如果取消注释函数
da,则以下代码不会编译B类。我使用完全相同的代码,并将其转换为
模板。从未在派生类中使用除基类类名
之外的任何模板参数,并且代码编译时没有任何关于
保护访问的投诉。为什么?有关模板化代码的不同之处?
In the following code, the class B will not compile if the function da is uncommented. I took exactly the same code, and turned it into templates. Never used the template parameter in anything but the baseclass name in the derived class, and the code compiles without any complaints about protected access. Why? What is different about the templated code?




尝试使用此主函数编译代码:


int main()

{

BT< int> b;

b.da(int());

}


你应该得到同样的错误。模板化版本

的原因是你的原始编译依赖名称(例如this-> a)

直到实例化才被查找,并且没有瞬间在你的例子中发生了




Jonathan



Try compiling your code with this main function:

int main()
{
BT<int> b;
b.da(int());
}

You should get the same error. The reason the templated version
compiles in your original that dependent names (such as this->a)
aren''t looked up until instantiation, and that no instantation occurs
in your example.

Jonathan




" ; Victor Bazarov <五******** @ comAcast.net>在消息中写道

新闻:SS ************* @ newsread1.dllstx09.us.to.veri o.net ...

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:SS*************@newsread1.dllstx09.us.to.veri o.net...
Steven T. Hatton写道:


模板化代码并未真正编译。除非您尝试实例化模板。你是否?


那么依赖/非依赖的区别是红鲱鱼吗?

代码的其余部分在哪里?
还有什么别的吗? ?如果没有,那么所有编译器都为你的模板做了检查语法。姓名不是
已解决,访问权限未被检查等。
Steven T. Hatton wrote:
The templated code is not really "compiled" unless you attempt to
instantiate the template. Did you?
So is the dependent/non-dependent distinction a red herring here?
Where is the rest of the code?
Was there anything else? If there wasn''t, then all the compiler did
for your templates was checking the syntax. Names were not resolved, access rights weren''t checked, etc.




Jonathan



Jonathan


这篇关于portected access:模板派生与类派生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆