gpt4 book ai didi

c++ 将 char* 转换为对象?

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:10:08 24 4
gpt4 key购买 nike


在 C++ 中是否可以将字符数组转换为对象,如下所示:

char* bytes = some bytes...
MyObject obj = (MyObject)(bytes);

?
我必须如何定义强制转换运算符?
谢谢 :)

最佳答案

您可能想为 MyObject 定义一个构造函数:

class MyObject {
public:
explicit MyObject(const char* bytes);
...
};

MyObject::MyObject(const char* bytes) {
// do whatever you want to initialize "MyObject" from the byte string
}

然后你就可以使用它了:

char* bytes = some bytes...
MyObject obj = MyObject(bytes); // this will work
MyObject obj(bytes); // so will this

关于c++ 将 char* 转换为对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5436092/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com