Gdiplus::Image::GetWidth() 和一堆其他 getter 不应该是“const”吗?

发布于 2024-07-15 01:31:51 字数 210 浏览 6 评论 0原文

为什么它们不是常量?

我认为这是 API 设计的缺陷。 或者我错过了什么?

UINT GetWidth();
UINT GetHeight();
...

UINT GetWidth() const;
UINT GetHeight() const;
...

Why aren't they const?

I believe that's flawed API design. Or am I missing something?

UINT GetWidth();
UINT GetHeight();
...

vs.

UINT GetWidth() const;
UINT GetHeight() const;
...

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

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

发布评论

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

评论(3

你的笑 2024-07-22 01:31:51

API 设计有缺陷? C 风格的 C++ 头文件? 来自为我们带来 CString 的团队? 不,不可能……

不过,说真的,不要指望 GoTW 级别的 C++在任何 Win32 API 中使用,或者不仅仅是围绕 C 样式句柄的基本包装。 Herb Sutter 一直忙于 .NET:ing C++,而不是改进 Microsoft 库设计。 WTL这是我所见过的最接近 Microsoft 接近现代 C++ 的情况,而且过着一种相当默默无闻的生活。

Flawed API design? C-style C++ headers? From the teams who brought us CString? Nah, can't be...

Seriously, though, don't expect GoTW-level C++ usage in any Win32 API, or more than a basic wrapper around the C-style handles. Herb Sutter has been busy with .NET:ing C++ rather than improving Microsoft library design. The WTL is as close as I've seen Microsoft come to modern C++, and that has led a rather obscure existence.

箹锭⒈辈孓 2024-07-22 01:31:51

很难说。 我同意,但也许实现中有些东西阻止它们成为 const,并且它们不想增加隐藏的开销。 现在我们有 mutable 关键字,但我认为它比这个更年轻API。

或者,API 的设计者可能属于(在我看来,有时数量惊人的)C++ 开发者群体,他们对 const 关键字怀有敌意,认为这只会让事情变得更难使用。 我不是 Windows 历史学家,所以我无法告诉你。 :)

Hard to say. I'd agree, but perhaps there is something in the implementation that prevents them from being const, and that they didn't want to add overhead to hide. Nowdays we have the mutable keyword, but I think that is younger than this API.

Or maybe the designers of the API belong to the (sometimes shockingly large, imo) bunch of C++ developers who are hostile towards the const keyword, feeling that only makes things harder to use. I'm no Windows historian, so I can't tell you. :)

遗弃M 2024-07-22 01:31:51

严格来说,你可能是对的 - 变量应该是 const。

我假设您正在谈论 Gdiplus C++ 本机 API。 如果您查看此代码和 Gdiplus 类的实现,您会发现大部分代码都是 Gdiplus Flat API 函数的基本包装器 (http://msdn.microsoft.com/en-us/library/ms533969(VS.85).aspx)。 这可能会导致很难使代码常量正确……或者(正如另一个人暗示的那样)微软通常不是非常现代的 C++。

编辑:看看 Gdiplus::Image::GetWidth() 的代码(在 GdiPlusBitmap.h 中),MS 可以很容易地使用 const 修饰符实现许多函数。 他们使用 Image::GetType() 完成此操作,并且内部代码与 Image::GetWidth()、Image::GetHeight() 几乎相同。

Strictly speaking you are probably right - the variable should be const.

I'm assuming you are talking about the Gdiplus C++ native API. If you look into the implementation of this code and the Gdiplus classes you'll find that most of the code is a basic wrapper around the Gdiplus Flat API functions (http://msdn.microsoft.com/en-us/library/ms533969(VS.85).aspx). This might make it either difficult to make the code const-correct... or (as another hinted at) it's Microsoft being typically not-very-modern-C++.

EDIT: Looking at the code for Gdiplus::Image::GetWidth() (in GdiPlusBitmap.h), it would have been easy for MS to implement many of the functions with the const modifier. They've done it with Image::GetType() and the code inside is pretty much identical to Image::GetWidth(), Image::GetHeight().

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