制作通过引用传递的句柄的本地副本 [英] making a local copy of a handle passed by reference

查看:56
本文介绍了制作通过引用传递的句柄的本地副本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将串行端口类的句柄传递给将配置串行端口的表单.
我可以将句柄传递给构造函数,并且可以按预期在构造函数内部正常工作,但是我需要指向句柄的本地副本,该副本指向构造函数完成更改串行端口参数后可以使用的端口.
我可以声明一个本地串行端口类var.
我可以将原始串行端口的类数据复制到该var中.
但是我想要原始的句柄,而不是其数据的本地副本.
这是一段代码,显示了我正在尝试做的事情

I’m trying to pass a handle to a serial port class to a form that will configure the serial port.
I can pass a handle to the constructor and it works fine/as I expect inside the constructor but I need a local copy of the handle that points to the port I can use after the constructor is finished to change the parameters of the serial port.
I can declare a local serial port class var.
I can copy of the original serial port’s class data to that var.
But I want a handle to the original not a local copy of its data.
Here is a snippet of code that shows what I"m trying to do

public ref class Config_Serial_Port_Form : public System::Windows::Forms::Form
{
public:
Config_Serial_Port_Form(System::IO::Ports::SerialPort^% initport)
{
initport->BaudRate = 600; //this accesses the correct port the way I want
theport = initport; //this only copies all the original to the local object 
//I need "theport" to be a pointer to the original not a copy fo the original 
InitializeComponent();
//
//TODO: Add the constructor code here
//
} 
void domystuff(void) //all this stuff works but only on my local copy of "theport" I want to do this to the original object instead
{
port_stuff^ port_stuff_ptr;
port_stuff_ptr = gcnew port_stuff(); 
this->Baud_Rate_comboBox->Text = port_stuff_ptr->BaudRate_to_string(theport);
this->Parity_comboBox->Text = port_stuff_ptr->Parity_to_string(theport);
this->handshakingcomboBox->Text = port_stuff_ptr->Handshake_to_string(theport);
this->databitscomboBox->Text = port_stuff_ptr->DataBits_to_string(theport);
if (theport->IsOpen)
this->portselectcomboBox->Text = theport->PortName;
this->portselectcomboBox->Items->AddRange(theport->GetPortNames());
this->portselectcomboBox->Items->Add("COM4");
}
protected:
/// summary;
/// Clean up any resources being used.
/// /summary
~Config_Serial_Port_Form()
{
if (components)
{
delete components;
}
}
private: System::IO::Ports::SerialPort^ theport; /// this gives me a second object not a handle to the original. 


private: System::IO::Ports::SerialPort^% theport;//I tried this but it won''t compile

private: System::IO::Ports::SerialPort^* theport;//I tried this too with no luck


How can I create a pointer to a handle?

推荐答案

您在概念上混淆了一些东西. "^"语法已经通过引用表示.
这是同时的插图和证明:

You are conceptually mixing up some things. A "^" syntax already means by reference.
Here is the illustration and the proof at the same time:

public ref class Port {
public:
    Port(int initialBaudRate) : BaudRate(initialBaudRate) {}
    int BaudRate;
}; //class Port

public ref class PortUser {
public:
    void ChangeBaudRate(Port^ port, int newRate) {
        port->BaudRate = newRate;
    }
}; //class PortUser

int main(array<System::String ^> ^args)
{
    PortUser portUser;
    Port^ port = gcnew Port(2400);
    portUser.ChangeBaudRate(port, 9600);
    Console::WriteLine(L"New baud rate = {0}", port->BaudRate);
    return 0;
} //main



输出:



Output:

New baud rate = 9600



那就是证明.如果函数ChangeBaudRatePort实例的(堆栈)副本一起使用,则旧值将为2400.

(很抱歉,我的示例违反了良好的封装/可见性编码样式,几乎没有什么实际意义;这对于说明按引用参数并不重要.)

在大多数方面,用gcnew实例化的.NET ref类型的行为类似于旧式C ++指针.主要区别在于此类指针易变(底层物理地址可能会发生变化)并且受垃圾回收的影响.在所有.NET语言集中,C ++的独特之处在于可以将值语义与引用类型一起使用.示例:在功能main中使用refPortUser.

运算符"%"的工作方式与地址"非常相似(即旧的"&"运算符的类似物).通过这种方式,它有助于从值到参考语义的过渡.考虑变体:



That is the proof. If the function ChangeBaudRate worked with the (stack) copy of the Port instance, you would get the old value of 2400.

(Sorry my sample violates good incapsulation/visibility coding styles and makes little practical sense; it does not matter for the purpose of the illustration of by-reference parameters.)

In most aspects .NET ref types instantiated with gcnew behave like old-style C++ pointers. The major difference is that such pointers are volatile (underlying physical address may change) and are subject to Garbage Collection. The uniqueness of C++ in a set of all .NET languages is the ability to use value semantic with reference types. Example: the use of the ref class PortUser in the function main.

The operator "%" works much like "address" (i.e. the analog of old "&" operator). In this way it help transition from value to reference semantics. Consider the variant:

int main(array<System::String ^> ^args)
{
    PortUser portUser;
    Port port(2400);
    portUser.ChangeBoudRate(%port, 9600);
    Console::WriteLine(L"New boud rate = {0}", port.BoudRate);
    return 0;
} //main



现在,我们在port实例中使用值语义,但是在调用ChangeBoudRate时,它再次表现为按引用参数. 9600再次让大家满意:).

谢谢.

—SA



Now we''re using value semantics for port instance, but in a call to ChangeBoudRate it again behaves like a by-reference parameter. 9600 again, to everyone''s satisfaction :).

Thank you.

—SA


SA
你当然是对的.
原来我是在愚弄自己的开发环境.
我正在更改代码中的属性,希望在下次运行代码时看到更改.
当我下次没有看到更改时,我以某种方式说服了我自己,我不是通过引用而是通过值传递,因为当我重新运行该程序时,包含的原始格式没有更改.
Hi SA
You are correct of course.
It turns out I was fooling my self with the development environment.
I was changing the properties in my code expecting to see the changes the next time I ran the code.
When I didn''t see the changes the next time around I somehow convinced my self that I was not passing by reference but only passing by by value becuase the origianl form that contained the was not changed when I reran the program.


这篇关于制作通过引用传递的句柄的本地副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆