即使我重载 = 运算符,也无法生成赋值运算符

发布于 2024-10-04 10:04:16 字数 289 浏览 4 评论 0原文

我的类是多态的,无论如何都不应该被用来 ='d。它有一个 Font& 类型的成员。因此编译器无法生成 = 运算符。所以我只是创建了赋值和复制构造函数的虚拟实现,将它们放在类的私有中,但它仍然警告我赋值运算符无法生成。我还能如何摆脱这个警告?

谢谢

警告9警告C4512:'AguiWidget':无法生成赋值运算符c:\ users \ josh \ Documents \ Visual Studio 2008 \ Projects \ agui \ alleg_5 \ agui \ aguiwidget.hpp 250

My class is polymorphic and should not be used to be ='d anyways. It has a member which is of type Font& and as a result the compiler cannot generate an = operator. So I just created dummy implementations of the assignment and copy constructor, put them in the private of the class, but it still warns me about assignment operator not able to get generated. How else can I get rid of this warning?

Thanks

Warning 9 warning C4512: 'AguiWidget' : assignment operator could not be generated c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\aguiwidget.hpp 250

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

把人绕傻吧 2024-10-11 10:04:16

编译器警告您的赋值运算符是您自己的类的赋值运算符。您现在拥有的是:

AguiWidget& operator=(const AguiFont &tmp);

您需要的是:

AguiWidget& operator=(const AguiWidget &tmp);

The assignment operator that the compiler is warning you about is the one for your own class. What you have now is:

AguiWidget& operator=(const AguiFont &tmp);

What you need is:

AguiWidget& operator=(const AguiWidget &tmp);
季末如歌 2024-10-11 10:04:16

您可以禁用它。当然,如果您确实尝试使用这些运算符,那么这是行不通的。

你确定你的签名正确吗?您是否为每个类、基类和派生类制作了它们?

You can disable it. This won't work, of course, if you're actually trying to use those operators.

Are you sure you got the signatures right? Did you make them for each class, base and deriveds?

破晓 2024-10-11 10:04:16

使用 boost::noncopyable

class AGUI_CORE_DECLSPEC AguiWidget : private boost::noncopyable

注意:

boost::noncopyable 也将对所有子类强制执行。

编辑:

哇...那是一门可怕的课程...

使用 pimpl idiom 来减少代码相互依赖性并提高界面可读性。

此外,您还应该尝试避免受保护的成员变量(尽可能多),因为它会破坏封装。

Use boost::noncopyable.

class AGUI_CORE_DECLSPEC AguiWidget : private boost::noncopyable

NOTE:

boost::noncopyable will be enforced for all sub-classes as well.

EDIT:

Wow... that's one scary class...

Use the pimpl idiom to reduce code interdependencies and improve interface readability.

Also you should try to avoid protected member variables (as much as possible) as it breaks encapsulation.

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