33,311
社区成员
发帖
与我相关
我的任务
分享
// HelloCpp.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <vector>
#include <iostream>
using namespace std;
class Fool {
public:
~Fool()
{
cout<<"Fool desconstructor called!"<<endl;
}
void say()
{
cout<<"hello"<<endl;
}
};
//为啥这个没有编译警告,arr不是自动变量么.
Fool * GetFool()
{
std::vector<Fool> arr(3);
return &arr[0];
}
//GetFool2 编译会有这些错误
///Warning 1 warning C4172: returning address of local variable or temporary
Fool * GetFool2()
{
Fool f;
return &f;
}
Fool * GetFool3()
{
Fool* f=new Fool;
return f;
}
int main() {
Fool* f= GetFool();
f->say();
system("PAUSE");
return 0;
}
class CA
{
public:
char p[2];
}
char * createCB()
{
CA a;
return a.p; // 这个当然是有问题的
}
class CA
{
public:
CA()
{
std::cout << "CA()!" << std::endl;
p = new char[2];
}
~CA()
{
std::cout << "~CA()!" << std::endl;
delete[] p;
}
char *p;
};
char * createCB()
{
CA a;
return a.p; // 这个当然是有问题的
}
wstring AnsiToUnicode(const char* buf)
{
int len = ::MultiByteToWideChar(CP_ACP, 0, buf, -1, NULL, 0);
if (len == 0) return L"";
std::vector<wchar_t> unicode(len);//看这里.这不是局部变量么?
::MultiByteToWideChar(CP_ACP, 0, buf, -1, &unicode[0], len);
return &unicode[0];//此处返回时wstring赋值操作符的操作,COPY了指针所指的字符串内容了么?
}
qp::StringW Global::AnsiToUnicode(const char* buf)
{
int len = ::MultiByteToWideChar(CP_ACP, 0, buf, -1, NULL, 0);
if (len == 0) return L"";
std::vector<wchar_t> unicode(len);//看这里.这不是局部变量么?
::MultiByteToWideChar(CP_ACP, 0, buf, -1, &unicode[0], len);
return &unicode[0];
}
std::vector<Fool>* GetFool()
{
std::vector<Fool> arr(3);
//return &arr[0];
return &arr;
}