经典的指针错误,在方法中,指针不要相互赋值,应该赋这个指针的内容。
2013-11-19 14:59
253 查看
#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++ 基类指针和子类指针相互赋值的实现方法
- 二维指针相互赋值 出现内存无法访问的解决方法
- C语言中常见的内存错误与解决方法(含野指针概念)
- oc -3 封装 弱语法(一个经典错误) 类方法
- Python捕捉命令输出、错误输出及赋值命令到变量的方法
- Incompatible types:'TDBGridEh' and 'TDBGrid' 出现这个错误的解决方法
- "C#类中虚方法相互调用的潜在重载错误"相关思考
- C/C++ 错误笔记-在给结构体中的指针赋值时,要注意该指针是否已指向内存空间
- C++经典错误之already defined in .obj解决方法之补充
- 关于Error 1 error C1189: #error : "include 'stdafx.h' before including this file这个错误的解决方法
- Call requires API level 16 (current min is 8 一个正常的代码出现这个错误提示: 解决方法: 在工程上右键 Android Tools | Clear
- jQuery实现选中弹出窗口选择框内容后赋值给文本框的方法
- 老大们,请问这个空指针错误是怎么产生的?
- IIS7.5 下:HTTP 错误 404.17 - Not Found 请求的内容似乎是脚本 解决方法
- vs2015启动网站调试提示 HTTP 错误 403.14 - Forbidden Web 服务器被配置为不列出此目录的内容。 解决方法
- 一组数据中只有一个数字出现了一次。 其他所有数字都是成对出现的。请找出这个数字(数组指针的方法)
- vs2015启动网站调试提示 HTTP 错误 403.14 - Forbidden Web 服务器被配置为不列出此目录的内容。 解决方法
- 最近电脑经常出现IE错误,具体解决方法请看以下内容。
- 知道指针地址 , 怎么取出指针指向的内容??? 0x7fff5fbff564 这种值 怎么赋值给一个变量??