64,643
社区成员
发帖
与我相关
我的任务
分享
CString str; str.Format( "(%d,%d)", x, y );
SetWindowText( str );
stringstream ss; ss<<"load failed, error:"<< errno;
throw runtime_error( ss.str() );
SetWindowText( txt( "(%d,%d)", x, y ) );
throw runtime_error( txt("load failed, error:%d", errno ) );
class txt_lock_inherit{
friend class txt;
txt_lock_inherit(){};
txt_lock_inherit(const txt_lock_inherit&){};
};
class txt:
public std::string,
private virtual txt_lock_inherit
{
public:
txt(){}
txt(const char* format, ...){
char buf[1024*4];
buf[ sizeof(buf)-1 ] = 0;
va_list arglist;
va_start(arglist, format);
_vsnprintf(buf, sizeof(buf)-1, format, arglist); // VC7,暂时没有处理可移植问题
std::string::assign(buf);
}
operator const char*()const{ return c_str(); }
};
class txt: public string{
public:
txt():string("hello world!\n"){};
operator const char*()const{ return c_str(); }
};
//我觉得私有继承string可以
//
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
class ConString: private std::string
{
public:
ConString():string("hello world!\n")
{
}
operator const char*() const
{
return this->c_str();
}
operator const std::string&() const
{
return *this;
}
friend inline std::ostream& operator<<(std::ostream& strm,const ConString& str)
{
strm << str.c_str();
return strm;
}
};
int main (){
string s1;
s1 = string(ConString());
cout<< s1 <<endl;
printf( ConString() ); //当成const char *
try
{
throw runtime_error( ConString() );
}
catch(const exception& e)
{
cout << e.what() <<endl;
}
cout << ConString() <<endl;
//ConString s2;
//s2="new";//不能通过
//s2=string("new");//不能通过
//ConString s3("de");//不能通过
return 0;
}
//但是 s1 = string(ConString());
// 和throw runtime_error( ConString() );
//这两句有警告:
//warning C4243: type cast conversion from 'class ConString *' to 'const class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &' exists, but is inaccessible
//vc6.0