Class C++/CLI:“;按钮^button1&引用;这里是什么意思?
我在C++/CLI中创建了一个WindowsForm类,其中包含一个按钮和onClick事件。我查看了源代码,看到了以下内容:Class C++/CLI:“;按钮^button1&引用;这里是什么意思?,class,visual-studio-2012,c++-cli,operator-keyword,Class,Visual Studio 2012,C++ Cli,Operator Keyword,我在C++/CLI中创建了一个WindowsForm类,其中包含一个按钮和onClick事件。我查看了源代码,看到了以下内容: public ref class MyForm : public System::Windows::Forms::Form { public: MyForm(void) { InitializeComponent(); // //TODO: Add the constructor code here
public ref class MyForm : public System::Windows::Forms::Form
{
public:
MyForm(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~MyForm()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
...
private: System::Void onClickButton1(System::Object^ sender, System::EventArgs^ e) {
}
}
公共引用类MyForm:public System::Windows::Forms::Form
{
公众:
我的表格(作废)
{
初始化组件();
//
//TODO:在此处添加构造函数代码
//
}
受保护的:
///
///清理所有正在使用的资源。
///
~MyForm()
{
if(组件)
{
删除组件;
}
}
私有:系统::Windows::窗体::按钮^button1;
...
私有:系统::Void onClickButton1(系统::对象^sender,系统::事件参数^e){
}
}
我想问:在类中声明按钮(button^button1;
)时,^
运算符的含义是什么?^
是:
句柄声明符(^,发音为“hat”)修改类型说明符,表示当系统确定对象不再可访问时,应自动删除已声明的对象
因此Button^
声明一个指向垃圾收集Button
对象的指针,该对象是使用而不是new
分配的:
ref new aggregate关键字分配类型的实例,该实例在对象变得不可访问时被垃圾收集,并向分配的对象返回句柄(^)
使用句柄声明符
^
声明的变量的行为类似于指向对象的指针。您可以使用->
访问该对象的成员。
CLR垃圾收集器机制确定对象是否不再被使用,是否可以删除,这意味着资源管理变得更容易。与C++中的原始指针不同,当你使用它们时,你需要使用<代码>删除< /代码>。代码>^指向垃圾收集的对象<代码> *>代码>指向一个不为对象的点。@ ReMyLeBeAui我在添加席之前添加了它。这不是C++(仅),而是C++ + CLI,是C++ C++.NET的微软派生。如果你想要真正的C++代码,你就不能使用WiFrm。