不允许模板参数成为朋友的基本原理? [英] Rationale behind not allowing template parameters to be friends?

查看:56
本文介绍了不允许模板参数成为朋友的基本原理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码:


============================== ===========

#include< iostream>


模板< class ValueT,OwnerT>

class Restricted_Public_Variable

{

public:

typedef Restricted_Public_Variable Self;

$ b $私人:

Restricted_Public_Variable()

:数据()

{

}


虚拟

~Restricted_Public_Variable()

{

}


Restricted_Public_Variable (const ValueT& arg)

:数据(arg)

{

}


Restricted_Public_Variable (const Self& arg)

:数据(arg.data)

{

}


const Self&

operator =(const ValueT& arg)

{

data = arg;

return *这个;

}


朋友OwnerT;


ValueT数据;


publ ic:

运营商ValueT()

{

返回数据;

}

const ValueT&

get()const

{

返回数据;

} < br $>
};


class Big

{

public:

Big(int arg)

:myint(arg)

{

}


Restricted_Public_Variable< int,Big> myint;

};


int

main()

{

Big b(3);


std :: cout<< b.myint<<的std :: ENDL; // 3


//以下是不允许的,我们可以

//只读''myint''值

// b.myint = 5;

}


==================== =====================

然而编译器(gcc 3.3.3)说:


模板参数不能成为朋友


Bah挫败...


有人可以解释这是为什么吗?


-
http:// www.it-is-truth.org/

Consider the following code:

=========================================

#include <iostream>

template<class ValueT, OwnerT>
class Restricted_Public_Variable
{
public:
typedef Restricted_Public_Variable Self;

private:
Restricted_Public_Variable()
: data()
{
}

virtual
~Restricted_Public_Variable()
{
}

Restricted_Public_Variable(const ValueT& arg)
: data(arg)
{
}

Restricted_Public_Variable(const Self& arg)
: data(arg.data)
{
}

const Self&
operator=(const ValueT& arg)
{
data = arg;
return *this;
}

friend OwnerT;

ValueT data;

public:
operator ValueT()
{
return data;
}

const ValueT&
get() const
{
return data;
}
};

class Big
{
public:
Big(int arg)
: myint(arg)
{
}

Restricted_Public_Variable<int, Big> myint;
};

int
main()
{
Big b(3);

std::cout << b.myint << std::endl; // 3

// The following is disallowed, we can
// only read the ''myint'' value
// b.myint = 5;
}

=========================================

Yet the compiler (gcc 3.3.3) says:

template parameters cannot be friends

Bah foiled...

Can anybody explain why this is?

--
http://www.it-is-truth.org/

推荐答案



" Asfand Yar Qazi" < im_not_giving_it_here@i_hate_spam.com>写在

消息新闻:c4 ********** @ news6.svr.pol.co.uk ...

"Asfand Yar Qazi" <im_not_giving_it_here@i_hate_spam.com> wrote in
message news:c4**********@news6.svr.pol.co.uk...
请考虑以下代码:



这是一篇关于这个主题的文章:
http://www.cuj.com/documents/s=8942/cujweb0312wilson/


Jonathan




" Asfand Yar Qazi" < im_not_giving_it_here@i_hate_spam.com>写在

消息新闻:c4 ********** @ news6.svr.pol.co.uk ...

"Asfand Yar Qazi" <im_not_giving_it_here@i_hate_spam.com> wrote in
message news:c4**********@news6.svr.pol.co.uk...
请考虑以下代码:



这是一篇关于这个主题的文章:
http://www.cuj.com/documents/s=8942/cujweb0312wilson/


Jonathan


" Asfand Yar Qazi" < im_not_giving_it_here@i_hate_spam.com>写在消息
"Asfand Yar Qazi" <im_not_giving_it_here@i_hate_spam.com> wrote in message
template< class ValueT,OwnerT>
class Restricted_Public_Variable
{
public:
typedef Restricted_Public_Variable Self;

私人:
Restricted_Public_Variable()
:data()
{
}


Restricted_Public_Variable(const ValueT& arg)
:data(arg)
{
}

> Restricted_Public_Variable(const Self& arg)
:data(arg.data)
{
}
const self&
operator =(const ValueT& arg)
{
data = arg;
return * this;
}


编译器生成的复制构造函数,operator =,析构函数是精细。如果你想让它们成为非内联的
,那么无论如何定义任何这些函数都是有意义的。我通常只为虚拟析构函数做这件事。


朋友OwnerT;

ValueT数据;


目前不允许这样做。我认为有一个建议让它合法化,

但也许我错了。作为一种解决方法,您可以创建一个公共函数

,它返回一个可写的数据引用,该函数将按值获取

OwnerT :: Restricted_Public_Variable或其他类。但是此类的默认

构造函数将是私有的,因此只有OwnerT

的成员才能创建OwnerT :: Restricted_Public_Variable对象。

ValueT& getdata(OwnerT :: Restricted_Public_Variable){

返回数据;

}


class Big
{
class Private_Restricted_Public_Variable {}; public:
Big(int arg)
:myint(arg)
{
}

Restricted_Public_Variable< int,Big>敏;
void f(){getdata(Private_Restricted_Public_Variable())= 3; }};
template<class ValueT, OwnerT>
class Restricted_Public_Variable
{
public:
typedef Restricted_Public_Variable Self;

private:
Restricted_Public_Variable()
: data()
{
}

virtual
~Restricted_Public_Variable()
{
}

Restricted_Public_Variable(const ValueT& arg)
: data(arg)
{
}

Restricted_Public_Variable(const Self& arg)
: data(arg.data)
{
}

const Self&
operator=(const ValueT& arg)
{
data = arg;
return *this;
}
Compiler generated copy constructor, operator=, destructor are fine. It
makes sense to define any of these functions anyway if you want them to be
non-inline. I usually do it for the virtual destructor only though.

friend OwnerT;

ValueT data;
It''s not allowed at present. I think there was a proposal to make it legal,
but maybe I am mistaken. As a workaround you can create a public function
that returns a writable reference to data, and the function will take an
OwnerT::Restricted_Public_Variable or other class by value. But the default
constructor of this class will be private so that only members of OwnerT
will be able to create OwnerT::Restricted_Public_Variable objects.

ValueT& getdata(OwnerT::Restricted_Public_Variable) {
return data;
}

class Big
{ class Private_Restricted_Public_Variable { }; public:
Big(int arg)
: myint(arg)
{
}

Restricted_Public_Variable<int, Big> myint; void f() { getdata(Private_Restricted_Public_Variable()) = 3; } };



这篇关于不允许模板参数成为朋友的基本原理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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