C++/CLI 有哪些属性?

发布于 2024-10-16 02:45:06 字数 77 浏览 1 评论 0原文

我在 C++ 代码中看到了术语 property。我认为它与 C++/CLI 有关。

到底是什么?

I saw in the term property in C++ code. I think it's connected to C++/CLI.

What is it exactly?

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

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

发布评论

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

评论(2

滥情稳全场 2024-10-23 02:45:06

它确实与 C++/CLI 有关(非托管 C++ 并没有真正的属性概念)。

属性是行为类似于字段的实体,但由 getter 和 setter 访问器函数在内部处理。它们可以是标量属性(它们的行为类似于字段)或索引属性(它们的行为类似于大批)。在旧语法中,我们必须直接在代码中指定 getter 和 setter 方法来实现属性 - 正如您可能猜到的那样,这并没有那么受欢迎。在 C++/CLI 中,语法更接近 C#,并且更易于编写和理解。

摘自本文:http://www.codeproject.com/KB/mcpp/CppCliProperties。另

请参阅 MSDN< /a> C++/CLI 中的属性。

示例代码:

private:
   String^ lastname;

public:
   property String^ LastName
   {
      String^ get()
      {
         // return the value of the private field
         return lastname;
      }
      void set(String^ value)
      {
         // store the value in the private field
         lastname = value;
      }
   }

It was indeed connected to C++/CLI (unmanaged C++ doesn't really have a notion of properties).

Properties are entities that behave like fields but are internally handled by getter and setter accessor functions. They can be scalar properties (where they behave like a field) or indexed properties (where they behave like an array). In the old syntax, we had to specify the getter and setter methods directly in our code to implement properties - wasn't all that well received as you might guess. In C++/CLI, the syntax is more C#-ish and is easier to write and understand.

Taken from this article: http://www.codeproject.com/KB/mcpp/CppCliProperties.aspx

Also see MSDN on properties in C++/CLI.

Sample code:

private:
   String^ lastname;

public:
   property String^ LastName
   {
      String^ get()
      {
         // return the value of the private field
         return lastname;
      }
      void set(String^ value)
      {
         // store the value in the private field
         lastname = value;
      }
   }
听闻余生 2024-10-23 02:45:06

是的,这确实是 Microsoft 版本的托管 C++ 代码或 C++/CLI。现在不仅还得写Get &设置方法,但您还需要将其定义为属性。 我会说,尽管我讨厌额外键入该属性的“只读”和“只写”版本,但它非常整洁。

但在非托管 c++ 中是不必要的!

例如,您可以在类中编写(将执行完全相同的操作!):

std::string GetLastName() const { return lastname;}
void SetLastName(std::string lName) { lastname = lName;}

“const”确保“GET”是只读的,并且集合是清晰的。无需定义属性或添加 String^ 与 std::string 的混淆......

Yep indeed this is Microsoft's version of managed c++ code or C++/CLI. Now not only do you still have to write Get & Set Methods but you also need to define it as a property. I will say as much as I hate the extra typing the 'Read Only' and 'Write Only' versions of the property is pretty neat.

But unnecessary in un-managed c++!!!

For instance you could write in a class (will do exactly the same thing!):

std::string GetLastName() const { return lastname;}
void SetLastName(std::string lName) { lastname = lName;}

The 'const' made sure it 'GET' was read only, and the set was clear. No need to define a property or add the confusion of String^ vs. std::string....

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