C++的新手.我见过人们通常在运算符重载时通过引用传递对象.好吧,我无法弄清楚什么时候真的有必要.如下面的代码所示,如果我在operator +中删除对象c1和c2的声明中的&符号,我仍然会得到相同的结果.当我们不想修改c1或c2时,在这种情况下是否有任何理由通过引用?
#include <iostream>
class Keys
{
private:
int m_nKeys;
public:
Keys(int nKeys) { m_nKeys = nKeys; }
friend Keys operator+(const Keys &c1, const Keys &c2);
int GetKeys() { return m_nKeys; }
};
Keys operator+(const Keys &c1, const Keys &c2)
{
return Keys(c1.m_nKeys + c2.m_nKeys);
}
int main()
{
Keys cKeys1(6);
Keys cKeys2(8);
Keys cKeysSum = cKeys1 + cKeys2;
std::cout << "There are " << cKeysSum.GetKeys() << " Keys." << std::endl;
system("PAUSE");
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有一个支持蓝牙的设备,其用户友好名称为“Sensor1”。该设备使用 SPP 配置文件。为了要求设备通过蓝牙开始数据流,我必须在与该设备对应的COM端口上写入“10111011”,如下所示:
ser = serial.Serial('COM5')
ser.write('10111011')
Run Code Online (Sandbox Code Playgroud)
问题是我不知道哪个 COM 端口对应于“Sensor1”。因此,我读取 Windows 注册表来获取设备名称:
import _winreg as reg
from itertools import count
key = reg.OpenKey(reg.HKEY_LOCAL_MACHINE, 'HARDWARE\\DEVICEMAP\\SERIALCOMM')
for i in count():
device, port = reg.EnumValue(key, i)[:2]
print "Device name \"%s\" found at %s" % (device, port)
Run Code Online (Sandbox Code Playgroud)
我得到的只是:
Device name \Device\Serial0 found at COM3
Device name \Device\BthModem16 found at COM4
Device name \Device\BthModem17 found at COM5
Run Code Online (Sandbox Code Playgroud)
如何获取设备名称,如下所示:
service = bluetooth.find_service()
print service["name"]
Run Code Online (Sandbox Code Playgroud)