前向声明不适用于转换运算符
考虑下一个代码:
#include <iostream>
using namespace std;
class B;
class A
{
public:
A() { p = 1;}
int p;
operator B() {B b; b.x = this->p; return b;}
};
class B
{
public:
int x;
};
int main()
{
A a;
B b = a;
return 0;
}
我试图将 A
转换为 B
,但我得到以下编译器尖叫声:
..\main.cpp:13: error: return type 'struct B' is incomplete
当我这样做时:
#include <iostream>
using namespace std;
class B
{
public:
int x;
};
class A
{
public:
A() { p = 1;}
int p;
operator B() {B b; b.x = this->p; return b;}
};
int main()
{
A a;
B b = a;
return 0;
}
代码编译了,但问题是:是否可以使用我上面写的前向声明来做到这一点?
非常感谢 罗南
Consider the next code :
#include <iostream>
using namespace std;
class B;
class A
{
public:
A() { p = 1;}
int p;
operator B() {B b; b.x = this->p; return b;}
};
class B
{
public:
int x;
};
int main()
{
A a;
B b = a;
return 0;
}
I'm trying to convert A
to B
, but I get the following compiler scream :
..\main.cpp:13: error: return type 'struct B' is incomplete
When I do this :
#include <iostream>
using namespace std;
class B
{
public:
int x;
};
class A
{
public:
A() { p = 1;}
int p;
operator B() {B b; b.x = this->p; return b;}
};
int main()
{
A a;
B b = a;
return 0;
}
the code compiles , but the question is : is it possible to do that using the forward declaration I wrote above ?
Much thanks
Ronen
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
是的,这是可能的,只要
A::operator B
的定义遵循class B
的定义。Yes, it is possible, as long as the definition of
A::operator B
follows the definition ofclass B
.不会。您的
A::operator B()
创建一个B
类型的对象。编译器需要知道B
的定义,以便能够创建B
类型的对象(例如,它需要知道执行 stack 的大小) - 指针计算。它需要知道是否需要调用自定义构造函数。)No. Your
A::operator B()
creates an object of typeB
. The compiler needs to know the definition ofB
in order to be able to create an object of typeB
(for instance, it needs to know how big it is to perform stack-pointer calculations. It needs to know whether custom constructors need to be called.)该行:
创建 B 的对象。这不可能发生,因为 B 未定义。
前向声明允许您声明指向对象的指针,这些对象可以在知道对象定义后创建,但您不能立即创建对象。
The line:
creates an object of B. This cannot happen since B isn't defined.
Forward declarations let you declare pointers to objects which can be created later when the object definition is known, but you can't create the objects straight off the bat.
不,这是不可能的。您不能声明未完全定义的类型的对象。
No, it's not possible. You cannot declare an object of type which is not completely defined.
您不能使用前向声明来执行此操作,因为编译器需要知道运算符返回类型的
B
(及其默认构造函数)的大小。如果没有完整的类型,它就无法知道大小。是的,它可能可以从您下面提供的定义中获取此信息,但由于历史原因,C 和 C++ 编译器仅考虑翻译单元中之前的定义。
You can't do this with the forward declaration because the compiler needs to know the size of
B
(and its default ctor as well) for the operator return type. Without a complete type, it cannot know the size.Yes, it probably could get this information from the definition you provide below, but for historical reasons, C and C++ compilers only consider definitions that come before in a translation unit.
如果不知道类的定义,就不可能创建类的对象。
将您的实现分为 .h 和 .cpp 文件。因此,您可以在
Ah
中有一个前向声明class B
并将其定义包含在A.cpp
中It is not possible to create an object of a class without knowing its definition.
Separate your implementation into .h and .cpp files. So you can have a forward declaration
class B
inA.h
and include its definition inA.cpp