[英]How to define class constructor inside another class header file?
This is a basic question but gives me hard time. 这是一个基本问题,但给我带来了困难。 I have class
A
and in the header file I want to define another class constructor B
from another header file. 我有
A
类A
并且要在头文件中从另一个头文件定义另一个类构造函数B
I tried this code below which I'm sure that's not the correct way. 我尝试了下面的代码,我确定这不是正确的方法。
Ah 啊
class A{
public:
A();
B b(); //Constructor from another Class that defined in another header file
void Operation();
};
I need to call the constructor B
in Ah
so I can call the constructor B
inside constructor A
and also use function in Class B
inside A::Operation()
. 我需要在
Ah
调用构造函数B
,以便可以在构造函数A
调用构造函数B
,还可以在A::Operation()
中的Class B
使用函数。
A.cpp 丙型肝炎
#include "A.h"
#include "B.h"
A::A{
b(); //call constructor b
}
void A::Operation(){
b.someFunction(); //use function from class B, error here
}
As expected, the error I got is at b.someFunction()
不出所料,我得到的错误是在
b.someFunction()
expression must have class type
表达式必须具有类类型
Anyone know how to define another class's constructor inside another class header file properly? 有谁知道如何在另一个类头文件中正确定义另一个类的构造函数? And call the other constructor inside main class constructor and use the other class's function globally?
并在主类构造函数中调用其他构造函数并全局使用其他类的函数? Sorry for basic and confusing question.
很抱歉出现基本且令人困惑的问题。
This is not a constructor: 这不是构造函数:
class A{ public: A(); B b(); //This is a function named b, returning an object of type B void Operation(); };
Same here: 同样在这里:
A::A{ b(); //call function b from above, that returns a B object }
And same here: 同样在这里:
void A::Operation(){ b.someFunction(); // Without invoking method b, you apply the dot operator on the method - which is illegal. }
You probably want an object of type B, and call someFunction
method on it. 您可能想要一个B类型的对象,并在其上调用
someFunction
方法。 Maybe you want: 也许你想要:
class A{
public:
A();
B b; // this is object named b, of type B
void Operation();
};
And then, if the constructor of B requires parameters, you can: 然后,如果B的构造函数需要参数,则可以:
A::A () : b(constructor parameters) {
}
If there is no need to pass parameters, you can just omit the construction of b, and the language will simply use the default constructor of B (without parameters). 如果不需要传递参数,则可以省略b的构造,而该语言将仅使用B的默认构造函数(不带参数)。
A::A () {
}
The way to invoke or call a constructor of B
is to create an instance of B
. 调用或调用
B
的构造函数的方法是创建B
的实例。
The process of creating an instance of B
involves calling/invoking a constructor of B
. 创建
B
实例的过程涉及调用/调用B
的构造函数。 The implementation (aka compiler) takes care of the mechanics of calling the constructor in the process of creating an object. 实现(也称为编译器)负责在创建对象的过程中调用构造函数的机制。 The job of the constructor is to ensure the object is initialised for subsequent use.
构造函数的工作是确保初始化该对象以供后续使用。
Practically speaking, it does not make sense to call a constructor in any context other than object construction. 实际上,在除对象构造之外的任何上下文中调用构造函数都是没有意义的。 There are some advanced use cases in which a constructor is effectively called manually (eg using a placement new expression to initialise a block of memory so it contains an object).
在某些高级用例中,可以有效地手动调用构造函数(例如,使用placement new表达式初始化内存块,使其包含对象)。 However that is done to create an object from the specified memory.
但是,这样做是为了从指定的内存创建对象。 This is completely different from what you are seeking.
这与您要寻找的完全不同。
To construct a B
within (code in) a header file ah
, the header file ah
needs to provide visibility of a declaration of B
and its constructors. 为了在头文件
ah
(输入)构造B
,头文件ah
需要提供B
声明及其构造函数的可见性。
For example, assuming bh
declares the class B
, the header ah
might do 例如,假设
bh
声明了B
类,则标头ah
可能会这样做
#include "b.h"
class A
{
public:
A() : b() {} ;
void Operation();
private:
B b; // A contains an instance of B
};
In the above (working bottom up) the declaration B b
specifies that A
has a member of type B
, named b
. 在上面(自下而上)中,声明
B b
指定A
具有类型B
的成员,名为b
。
The definition of A
s constructor A
s构造函数的定义
A() : b() {} ;
uses an initialiser list to initialise (construct) that member b
. 使用初始化程序列表初始化(构造)该成员
b
。 This assumes the class B
has a constructor that can accept no arguments. 假设类
B
的构造函数不能接受任何参数。
The declaration you had within class A
(which I have removed from the sample I provided above) 您在
A
类中拥有的声明(已从上面提供的示例中删除了该声明)
B b(); //Constructor from another Class that defined in another header file
is not what you describe. 不是你所描述的。 It is actually a declaration of a member function of class
A
, which is named b()
and returns a B
. 它实际上是类
A
的成员函数的声明,该类名为b()
并返回B
Calling such a function generally requires B
to have a working constructor (typically a copy or move constructor). 调用此类函数通常需要
B
具有有效的构造函数(通常是副本或move构造函数)。 Such a declaration, however, is not a constructor of B
. 但是,这样的声明不是
B
的构造函数。
just #include "Bh"
in Ah and have B b;
仅在Ah中
#include "Bh"
并拥有B b;
object in Ah . 对象在Ah中 。
#include "B.h"
class A{
A();
B b;
void operation();
}
this way the compiler will have all the information needed from class B
constructors and other functions as well for this compilation unit A.cpp and linker will do its job later linking the function name to its logic in other compilation unit B.cpp . 这样,编译器将获得
class B
构造函数和其他函数所需的所有信息,也适用于此编译单元A.cpp ,链接器将在以后将函数名称链接至其他编译单元B.cpp中的逻辑来完成其工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.