#include <string>
#include <iostream>
class message {
public:
void setPayload(std::string* s) {
m_str = s;
}
void getPayload(std::string* s) {
s = m_str;
}
private:
std::string* m_str;
};
int main(int argc, char* argv[]) {
message m;
std::string *s = new std::string("hello");
m.setPayload(s);
std::string* t = new std::string();
m.getPayload(t);
std::cout << *t << std::endl;
}
#include <iostream>
class message {
public:
void setPayload(std::string* s) {
m_str = s;
}
void getPayload(std::string* s) {
s = m_str;
}
private:
std::string* m_str;
};
int main(int argc, char* argv[]) {
message m;
std::string *s = new std::string("hello");
m.setPayload(s);
std::string* t = new std::string();
m.getPayload(t);
std::cout << *t << std::endl;
}
本文介绍了一个简单的C++程序示例,演示了如何通过类实现字符串消息的设置和获取功能。具体包括了类成员函数setPayload和getPayload的使用方法,并在主函数中进行了实例展示。

被折叠的 条评论
为什么被折叠?



