循环包含/前向定义总是可以解决的? [英] Cyclic includes / forward definitions always solvable?

查看:42
本文介绍了循环包含/前向定义总是可以解决的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

关于类的循环依赖关系(比如

类A引用B类而B类引用A类):它们可以全部通过前向定义类来解决
,并通过在类定义之外实现实际的

函数,或者仍然存在

不可能的情况以这种方式解决并可能需要重写你的代码/

重写你的类层次结构?

Hi all,
Regarding those cyclic dependencies of classes (like cases in which
class A refers to class B and class B to class A): can they ALL be
resolved by forward defining classes, and by implementing the actual
functions outside the class definition, OR there are STILL cases which
cannot be solved in this way and could require rewriting your code /
rewriting your class hierarchy?

推荐答案

3月1日星期一2004 13:14:30 -0600,John Doe< jo ***** @ nowhere.com>写道:
On Mon, 01 Mar 2004 13:14:30 -0600, John Doe <jo*****@nowhere.com> wrote:
大家好,
关于类的循环依赖(例如,类A指的是B类,B类指的是A类):它们是否可以通过前向定义类来解决,并通过在类定义之外实现实际的
函数来解决,或者仍然存在以这种方式无法解决的情况并且可能需要重写代码/
重写你的类层次结构?
Hi all,
Regarding those cyclic dependencies of classes (like cases in which
class A refers to class B and class B to class A): can they ALL be
resolved by forward defining classes, and by implementing the actual
functions outside the class definition, OR there are STILL cases which
cannot be solved in this way and could require rewriting your code /
rewriting your class hierarchy?




解析循环依赖与你放置

实际函数实现的位置无关(包括内联函数,

,因为它们可以定义为非原位);它确实与编辑器是否已经看到完整定义或者是否为b b $ b有关。在那一点上的班级

,其中需要该信息。通常,感兴趣的信息

是该类实例的存储要求。


注意,即使它定义为类定义也会被认为是完整的/>
包含/指向不完整类型的指针/。


据我所知,这一切归结为:你不能写任何

声明或声明,要求编译器在您提供该类的完整定义之前知道一个类有多大。

-leor


Leor Zolman

BD软件
le ** @ bdsoft.com
www.bdsoft.com - C / C ++现场培训,Java,Perl& Unix

C ++用户:下载BD Software的免费STL错误消息

Decryptor at www.bdsoft.com/tools/stlfilt.html



Resolving cyclic dependencies has nothing to do with where you put the
actual function implementations (including in the case of inline functions,
since they can be defined "ex-situ"); it does have to do with whether or
not the compiler has seen a "complete definition" of the class at the point
where that information is required. Typically, the information of interest
is the storage requirement for instances of that class.

Note that a class definition would be considered complete even if it
includes /pointers to incomplete types/.

As far as I know, it all boils down to this: you cannot write any
declaration or statement that requires the compiler to know how big a class
is before you''ve provided a complete definition of that class.
-leor

Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software''s free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html


" John Doe" <乔***** @ nowhere.com>在消息中写道

news:c2 ********** @ news.vanderbilt.edu ...
"John Doe" <jo*****@nowhere.com> wrote in message
news:c2**********@news.vanderbilt.edu...
关于类的循环依赖关系(如案例)其中,A类引用B类,B类引用A类):它们是否可以通过前向定义类来解析,并通过在类定义之外实现实际的
函数来实现,或者有些情况无法通过这种方式解决,可能需要重写代码/重写您的类层次结构吗?
Regarding those cyclic dependencies of classes (like cases in which
class A refers to class B and class B to class A): can they ALL be
resolved by forward defining classes, and by implementing the actual
functions outside the class definition, OR there are STILL cases which
cannot be solved in this way and could require rewriting your code /
rewriting your class hierarchy?




解决方法是总是有可能,但仍然有一些需要改变设计的难以处理的案件。

例如,你将如何使用以下内容:


struct A {

std :: list< B> myBs;

};


struct B {

std :: list< A> myAs;

};


对于下一个C ++标准,我认为它被认为是明确允许的实例化

的一些标准容器(例如std :: vector)

类型不完整。

但目前还没有这样的保证。 />

另一个经典问题是:如何定义函数f(void)

返回指向f(void)的指针?


解决方法是使用中间结构:

struct FP {

FP(* fp)(void);

};


FP f(无效)

{

FP ans = {f};

返回ans;

}


这个成语实际上在一些实现中很有用

状态机。


问候,

伊万

-
http://ivan.vecerina.com/contact/?subject=NG_POST < - 电子邮件联系表格



A workaround is always possible, but there are still some
tough cases that require an alteration of the design.
For example, how would you use the following:

struct A {
std::list<B> myBs;
};

struct B {
std::list<A> myAs;
};

For the next C++ standard, I believe that it is being
considered to explicitly allow the the instantiation
of some standard containers (e.g. std::vector)
with incomplete types.
But there is no such guarantee currently.

Another classic problem is: how do you define a function f(void)
that returns a pointer to an f(void) ?

A workaround is to use an intermediate structure:
struct FP {
FP (*fp)(void);
};

FP f(void)
{
FP ans = {f};
return ans;
}

This idiom is actually useful in some implementations
of a state machine.

Regards,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form


Ivan Vecerina写道:
Ivan Vecerina wrote:
" John Doe" <乔***** @ nowhere.com>在消息中写道
新闻:c2 ********** @ news.vanderbilt.edu ...
"John Doe" <jo*****@nowhere.com> wrote in message
news:c2**********@news.vanderbilt.edu...
关于类的循环依赖(如案例)其中,A类引用B类,B类引用A类):它们是否可以通过前向定义类来解析,并通过在类定义之外实现实际的
函数来实现,或者有些情况无法以这种方式解决,可能需要重写代码/重写您的类层次结构吗?
Regarding those cyclic dependencies of classes (like cases in which
class A refers to class B and class B to class A): can they ALL be
resolved by forward defining classes, and by implementing the actual
functions outside the class definition, OR there are STILL cases which
cannot be solved in this way and could require rewriting your code /
rewriting your class hierarchy?



一个变通方法总是可行的,但是还有一些难以处理的案例需要改变设计。

例如,你将如何使用以下内容:

struct A {
std :: list< B> myBs;
};

struct B {
std :: list< A> myAs;
};


A workaround is always possible, but there are still some
tough cases that require an alteration of the design.
For example, how would you use the following:

struct A {
std::list<B> myBs;
};

struct B {
std::list<A> myAs;
};




我不是特别了解这个例子的用处。


A是一个包含B'列表的strcuture,它依次包含一个A'的
列表,其中包含一个B'列表,其中包含一个列表/>
A'的.....,你得到的照片。


我认为编译器不允许这么可怕是一件好事br />
东西......



I don''t particularly understand the usefulness of this example.

A is a strcuture that contains a list of B''s, which in turns contains a
list of A''s, which contains a list of B''s, which conains a list of
A''s....., you get the picture.

I think it IS a good thing that compilers don''t allow such horrible
things...


这篇关于循环包含/前向定义总是可以解决的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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