MC ++类中的Pimpl习语 [英] Pimpl idiom in MC++ classes

查看:88
本文介绍了MC ++类中的Pimpl习语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为在MC ++中无法识别朋友,所以在MC ++中使用pImpl惯用语似乎几乎不可能使用
类。通常,pImpl类是

类的朋友,它为其提供私有实现,因此它可以访问任何受保护的成员,包括继承的受保护的成员成员,那个

级。如果没有''朋友'',pImpl类就不能再这样做了,而且它是一个传递必要的受保护数据或受保护成员函数的
PITA指向pImpl成语的
指针成员函数每次可能需要它。


在MC ++中是否有一个很好的解决方法?

解决方案

< blockquote> Edward Diener写道:

因为朋友在MC ++中无法识别,所以使用MC ++中的pImpl习语似乎几乎不可能。通常,pImpl类是
类的朋友,它为其提供私有实现,以便它可以访问任何受保护的成员,包括继承的受保护成员,
类。如果没有''朋友'',pImpl类就不能再这样做了,并且每次需要它时,它都会传递必要的受保护数据或受保护成员函数指向pImpl成语函数的指针。 。

在MC ++中有没有一个很好的解决方法?




看起来我的大部分pImpl类都不需要他们的朋友

" host"类,但无论如何,没有必要让

pImpl类成为朋友,因为它可以完全访问宿主类。 ISTR,

VC7跟踪此DR:

http://www.comeaucomputing.com/iso/cwg_defects.html#45


因此以下内容应该有效:


等级X

{

私人:


等级Y;

Y * y;


struct Z {};


void g(Z&);


public:


void f();

};


class X :: Y

{

私人:


结构D:X :: Z {};


public:


void f(X& x)

{

X :: Z z;

xg(z);

}

};


void X :: f()

{

y-> f(* this);

}


-

Doug Harrison

Microsoft MVP - Visual C ++


Doug Harrison [MVP]写道:

取值o以下应该有效:

第X类
{
私人:

Y类;
Y * y;

struct Z {};

void g(Z&);

public:

void f();
};

类X :: Y
{
私人:

结构D:X :: Z {};
<公开:

void f(X& x)
{X / Z z;
xg(z);
}
};

void X :: f ()
{
y-> f(* this);
}




对不起,这是一个例子对于常规C ++类,而不是__gc类。要获得

后者,添加必要的__gc'并修复在X :: Y :: f中

''z''的声明和用法。 br />

-

Doug Harrison

Microsoft MVP - Visual C ++


Doug Harrison [MVP]写道:

Doug Harrison [MVP]写道:

所以以下应该有效:

类X
{
私人:

Y类;
Y * y;

结构Z {};

void g(Z&);

公开:

void f();
};

类X :: Y
{
私人:

结构D:X :: Z {};


我不认为这应该有用。 X无法访问X :: Z,这是对于X私有的


公开:

void f(X& x)
{
X :: Z z;
xg(z);


同上。无法访问X :: Z或xg,两者都是私有的。

}
};

void X :: f()
{
y-> f(* this);
}



抱歉,这是常规C ++类的示例,而不是__gc类。<为了获得后者,添加必要的__gc'并修复声明
以及在X :: Y :: f中使用''z'。




我从来没有将pImpl用作嵌套类,而是作为一个单独的类

,它是其宿主类的朋友。但即使在嵌套类的情况下,

嵌套类也无法访问其周围类的私有或受保护成员

。除非规则以某种方式发生了巨大变化。


Because ''friend'' is not recognized in MC++, using the pImpl idiom in MC++
classes seems nearly impossible. Normally a pImpl class is a ''friend'' to the
class for which it supplies the private implementation, so that it can
access any protected members, including inherited protected members, of that
class. Without ''friend'' the pImpl class can no longer do this, and it is a
PITA passing the necessary protected data or protected member function
pointers to the pImpl idiom member functions each time it may need it.

Is there a good workaround for this in MC++ ?

解决方案

Edward Diener wrote:

Because ''friend'' is not recognized in MC++, using the pImpl idiom in MC++
classes seems nearly impossible. Normally a pImpl class is a ''friend'' to the
class for which it supplies the private implementation, so that it can
access any protected members, including inherited protected members, of that
class. Without ''friend'' the pImpl class can no longer do this, and it is a
PITA passing the necessary protected data or protected member function
pointers to the pImpl idiom member functions each time it may need it.

Is there a good workaround for this in MC++ ?



It seems like most of my pImpl classes don''t need to be friends of their
"host" classes, but in any event, it shouldn''t be necessary to make the
pImpl class a friend for it to have full access to the host class. ISTR that
VC7 tracks this DR:

http://www.comeaucomputing.com/iso/cwg_defects.html#45

So the following should work:

class X
{
private:

class Y;
Y* y;

struct Z {};

void g(Z&);

public:

void f();
};

class X::Y
{
private:

struct D : X::Z {};

public:

void f(X& x)
{
X::Z z;
x.g(z);
}
};

void X::f()
{
y->f(*this);
}

--
Doug Harrison
Microsoft MVP - Visual C++


Doug Harrison [MVP] wrote:

So the following should work:

class X
{
private:

class Y;
Y* y;

struct Z {};

void g(Z&);

public:

void f();
};

class X::Y
{
private:

struct D : X::Z {};

public:

void f(X& x)
{
X::Z z;
x.g(z);
}
};

void X::f()
{
y->f(*this);
}



Sorry, that''s an example for regular C++ classes, not __gc classes. To get
the latter, add the necessary __gc''s and fix the declaration and usage of
''z'' in X::Y::f.

--
Doug Harrison
Microsoft MVP - Visual C++


Doug Harrison [MVP] wrote:

Doug Harrison [MVP] wrote:

So the following should work:

class X
{
private:

class Y;
Y* y;

struct Z {};

void g(Z&);

public:

void f();
};

class X::Y
{
private:

struct D : X::Z {};
I don''t think this should work. X does not have access to X::Z which is
private to X

public:

void f(X& x)
{
X::Z z;
x.g(z);
Ditto. No access to X::Z or x.g, both of which are private.
}
};

void X::f()
{
y->f(*this);
}



Sorry, that''s an example for regular C++ classes, not __gc classes.
To get the latter, add the necessary __gc''s and fix the declaration
and usage of ''z'' in X::Y::f.



I had never used pImpl as a nested class, but rather as a separate class
which is a friend to its host class. But even in the nested class situation,
a nested class does not have access to the private or protected members of
its surrounding class. Unless the rules have changed drastically somehow.


这篇关于MC ++类中的Pimpl习语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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