前向声明不适用于转换运算符

发布于 2024-12-02 06:40:18 字数 776 浏览 2 评论 0原文

考虑下一个代码:

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

愁杀 2024-12-09 06:40:18

是的,这是可能的,只要A::operator B的定义遵循class B的定义。

#include <iostream>
using namespace std;


class B;

class A
{
public:

    A() { p = 1;}
    int p;
    operator B();
};


class B
{
public:
    int x;
};

inline A::operator B() {B b; b.x = this->p; return b;}


int main()
{

    A a;
    B b = a;
    return 0;
}

Yes, it is possible, as long as the definition of A::operator B follows the definition of class B.

#include <iostream>
using namespace std;


class B;

class A
{
public:

    A() { p = 1;}
    int p;
    operator B();
};


class B
{
public:
    int x;
};

inline A::operator B() {B b; b.x = this->p; return b;}


int main()
{

    A a;
    B b = a;
    return 0;
}
迟到的我 2024-12-09 06:40:18

不会。您的 A::operator B() 创建一个 B 类型的对象。编译器需要知道 B 的定义,以便能够创建 B 类型的对象(例如,它需要知道执行 stack 的大小) - 指针计算。它需要知道是否需要调用自定义构造函数。)

No. Your A::operator B() creates an object of type B. The compiler needs to know the definition of B in order to be able to create an object of type B (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.)

少女情怀诗 2024-12-09 06:40:18

该行:

operator B() {B b; return b;}

创建 B 的对象。这不可能发生,因为 B 未定义。

前向声明允许您声明指向对象的指针,这些对象可以在知道对象定义后创建,但您不能立即创建对象。

The line:

operator B() {B b; return b;}

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.

梦断已成空 2024-12-09 06:40:18

不,这是不可能的。您不能声明未完全定义的类型的对象。

No, it's not possible. You cannot declare an object of type which is not completely defined.

就此别过 2024-12-09 06:40:18

您不能使用前向声明来执行此操作,因为编译器需要知道运算符返回类型的 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.

暮年 2024-12-09 06:40:18

如果不知道类的定义,就不可能创建类的对象。

将您的实现分为 .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 in A.h and include its definition in A.cpp

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文