我想要一个函数来枚举 Windows 上的串行 (COM) 端口。为此,我主要从http://www.codeguru.com/cpp/wp/system/hardwareinformation/article.php/c5721/Determining-What-Serial-Ports-Are-Available-on-a-Windows-复制代码机器.htm
在头文件中:
#include "SerialPort.h"
#include <list>
#include <objbase.h>
#include <initguid.h>
#include <Setupapi.h>
typedef std::list<SerialPort> PortList;
class SerialConnection
{
private:
static PortList availible_ports;
public:
static void enumerateSerialPorts(bool);
static const PortList& getPortList(){ return availible_ports; }
}
Run Code Online (Sandbox Code Playgroud)
执行:
void SerialConnection::enumerateSerialPorts(bool check)
{
availible_ports.clear();
CString strErr;
// Create a device information set that will be the container for
// the device interfaces.
GUID *guidDev = (GUID*)&GUID_CLASS_COMPORT;
HDEVINFO hDevInfo = INVALID_HANDLE_VALUE;
SP_DEVICE_INTERFACE_DETAIL_DATA *pDetData = NULL;
try {
hDevInfo …Run Code Online (Sandbox Code Playgroud) 我想尝试这样的事情:
int main()
{
class Base
{
public:
Base() = default;
virtual void print() = 0;
void CallThread()
{
std::thread(&Base::print, *this);
};
};
class derived1 : public Base
{
public:
derived1() = default;
void print() { printf("this is derived1\n"); }
};
class derived2 : public Base
{
public:
derived2() = default;
void print() { printf("this is derived2\n"); }
};
Base* ptr;
ptr = new derived1();
ptr->CallThread();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想要的结果是:
这是派生的1.
问题是它甚至不会编译.我正在使用VisualStudio 2013.
错误是:
错误2错误C3640:'main :: Base :: [thunk]:__ thiscall …
我根据C++ 11带来的创新,即uchar16_t/u16string来提出这个问题.
我写了一个应该有多语言支持的应用程序.根据我的计划,本地化字符串将以XML格式存储为UTF-16,并使用pugixml进行检索.这些字符串既可用于GUI,也可用于生成计算结果的HTML报告.由于我已经将wchar_t/wstring理解为不赞成使用新的u16string,因此我计划使用u16string在程序中存储语言字符串.但是既然pugixml和MFC的CString都使用wchar_t作为Unicode的下划线存储类型,我现在应该忘记u16string而是直接使用wstring吗?
语言可移植性至关重要,平台可移植性无关紧要.
我使用MVS 2013和英特尔编译器.
考虑以下课程:
class MyClass
{
private:
const unsigned int num;//num identifies the object. needs to be const
unsigned int checkNum(unsigned int);// verifies that num has a valid value
public:
MyClass(unsigned int n): num(checkNum(n)) {}
};
unsigned int MyClass:checkNum(unsigned int n)
{
if (some_condition)
throw std::invalid_argument("Invalid number");
return n;
}
Run Code Online (Sandbox Code Playgroud)
难点在于,try由于范围检查,必须在块内构造对象:
int main()
{
try {
MyClass mc(1000);
}
catch (std::invalid_argument &ia)
{
std::cout << ia.what();
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
问题是mc在try块之外是不可用的.
可能的解决方案:
try在使用的整个范围内扩展块mc.在许多情况下不实用. …
c++ ×3
c++11 ×2
const ×1
constructor ×1
exception ×1
linker ×1
polymorphism ×1
serial-port ×1
setupapi ×1
try-catch ×1
unicode ×1
visual-c++ ×1
windows ×1