C++ 类原型设计

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6187974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:39:44  来源:igfitidea点击:

Class prototyping

c++prototypeclass-design

提问by Skeith

I have put several instances of class b in class a but this causes an error as class a does not know what class b is.

我已经将类 b 的几个实例放在类 a 中,但这会导致错误,因为类 a 不知道类 b 是什么。

Now I know I can solve this problem by writing my file b a c but this messes up the reachability as well as annoys me. I know I can prototype my functions so I do not have this problem but have been able to find no material on how to prototype a class.

现在我知道我可以通过编写我的文件 bac 来解决这个问题,但这会破坏可访问性并使我烦恼。我知道我可以对我的函数进行原型设计,所以我没有这个问题,但是找不到关于如何对类进行原型设计的材料。

does anyone have an example of class prototyping in c++.

有没有人有 C++ 类原型的例子。

as there seems to be some confusion let me show you what i want

因为似乎有些混乱让我告诉你我想要什么

class A
{
public:

B foo[5];

};

class B
{
public:
int foo;
char bar;
}

but this does not work as A cannot see B so i need to put something before them both, if it was a function i would put A(); then implement it later. how can i do this with a class.

但这不起作用,因为 A 看不到 B,所以我需要在他们两个之前放一些东西,如果它是一个函数,我会放 A(); 然后稍后实施。我怎么能在课堂上做到这一点。

回答by Kanopus

You can declare all your classes and then define them in any order, like so:

您可以声明所有类,然后按任意顺序定义它们,如下所示:

// Declare my classes
class A;
class B;
class C;

// Define my classes (any order will do)
class A { ... };
class B { ... };
class C { ... };

回答by Puppy

You're looking for declarations.

您正在寻找声明。

class A;
class B {
    A MakeA();
    void ProcessA(A a);
};
class A {
    B bs[1000];
};

If you forward declare a class, you can

如果你转发声明一个类,你可以

declare functions taking and returning it or complex types made of it
declare member variables of pointer or reference to it

This basically means that in any case which doesn't end up with instances of A inside B and vice versa, you should be able to declare and define any interface between A and B.

这基本上意味着,在任何情况下,在 B 中没有 A 的实例,反之亦然,您应该能够声明和定义 A 和 B 之间的任何接口。

回答by Bj?rn Pollex

The usual way to resolve circular dependencies is to use a forward declaration:

解决循环依赖的常用方法是使用前向声明:

// Bar.h

class Foo; // declares the class Foo without defining it

class Bar {
    Foo & foo; // can only be used for reference or pointer
};

// Foo.h

#include <Bar.h>

class Foo {
    Bar bar; // has full declaration, can create instance
}

You can provide a full declaration and definition in another file. Using the forward declaration, you can create pointers and references to the class, but you cannot create instances of it, as this requires the full declaration.

您可以在另一个文件中提供完整的声明和定义。使用前向声明,您可以创建类的指针和引用,但不能创建它的实例,因为这需要完整的声明。

回答by Arunmu

class b;

class b;

class a {
public:
     b * inst1;
};
class b{
....
};
class a {
public:
     b * inst1;
};
class b{
....
};

Is this what you needed ?

这是你需要的吗?