如何处理相互依赖的类 [英] How to handle mutually dependent classes

查看:114
本文介绍了如何处理相互依赖的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在开展的一个项目中,我发现自己有两个类,即
互相引用。例如:


firstclass.h:

{

class FirstClass

{

函数(SecondClass *指针);

}

}


secondclass.h

{

class SecondClass

{

function(FirstClass * Pointer);

}

}


可悲的是,我不能想到一个更好的解决方案,我想要做的事情。

不幸的是,我来了显而易见的问题是,每个类都需要

其他人才能在声明之前声明它。


有没有优雅的方法来处理这个问题,或者我是否回到绘图

板并想出更好的解决方案?


Brad Kartchner

In a project I am working on, I have found myself with two classes that
reference each other. For example:

firstclass.h:
{
class FirstClass
{
function (SecondClass* Pointer);
}
}

secondclass.h
{
class SecondClass
{
function (FirstClass* Pointer);
}
}

Sadly, I can''t think of a better solution for what I am trying to do.
Unfortunately, I come up with the obvious problem that each class needs the
other one to be declared BEFORE it can be declared itself.

Is there any elegant way to handle this, or do I go back to the drawing
board and think up a better solution?

Brad Kartchner

推荐答案

Brad Kartchner发布:
Brad Kartchner posted:
在我正在进行的项目中,我发现自己有两个相互引用的课程。例如:

firstclass.h:
类FirstClass
{
函数(SecondClass *指针);
}
}

secondclass.h
{class class SecondClass
{
函数(FirstClass *指针);
}
}

可悲的是,我不能想到一个更好的解决方案来解决我想做的事情。
不幸的是,我想出了每个班级需要的明显问题
另一个在被声明之前就被声明了。

有没有优雅的方法来处理这个问题,还是我回到绘图板并想出一个更好的解决方案?

Brad Kartchner
In a project I am working on, I have found myself with two classes that
reference each other. For example:

firstclass.h:
{
class FirstClass
{
function (SecondClass* Pointer);
}
}

secondclass.h
{
class SecondClass
{
function (FirstClass* Pointer);
}
}

Sadly, I can''t think of a better solution for what I am trying to do.
Unfortunately, I come up with the obvious problem that each class needs
the other one to be declared BEFORE it can be declared itself.

Is there any elegant way to handle this, or do I go back to the drawing
board and think up a better solution?

Brad Kartchner



B级;


A级

{

B monkey;

}

B级

{

A ape ;

}


TTTTTTThhhhhhhheeeeeeerrrrreeeeeeeee ya go

-JKop


class B;

class A
{
B monkey;
}
class B
{
A ape;
}

TTTTTTThhhhhhhhheeeeeeerrrrreeeeeeeee ya go
-JKop


JKop< NU ** @ null.null>这样说:
JKop <NU**@null.null> spoke thus:
B类;




为了OP的利益,这被称为前向声明。 />

-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。



For OP''s benefit, this is called a forward declaration.

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


" Brad Kartchner" < LA *********** @ qwest.net>在消息新闻中写道:gvykc.37
"Brad Kartchner" <la***********@qwest.net> wrote in message news:gvykc.37
在我正在进行的项目中,我发现自己有两个相互引用的课程。例如:


使用前向声明。

firstclass.h:


class SecondClass;

//此行声明存在名为SecondClass的类

//稍后将定义

//但至少可以声明指针和引用SecondClass

{
类FirstClass
{
函数(SecondClass *指针);
}
}

secondclass.h


class FirstClass; //在这里添加前向声明

{
类Sec​​ondClass
{
函数(FirstClass *指针);
}
}
In a project I am working on, I have found myself with two classes that
reference each other. For example:
Use forward declarations.
firstclass.h:
class SecondClass;
// this line declare that there exists class named SecondClass
// it will be defined later
// but at least you can declare pointers and reference to SecondClass
{
class FirstClass
{
function (SecondClass* Pointer);
}
}

secondclass.h
class FirstClass; // add a forward declaration here too
{
class SecondClass
{
function (FirstClass* Pointer);
}
}






这篇关于如何处理相互依赖的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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