重载运算符+/char * ch1 + char * ch2 [英] overload operator+ / char* ch1 + char* ch2
问题描述
我创建了带有一些额外功能的简单字符串类-主要用于学习目的. 现在,我想重载+运算符,以允许我在字符串中添加两个或多个char *.
I've created simple string class with some extra functions - its mainly for learning purposes. Now i want to overload operator + to allow me add two or more char* to my string.
这就是我要做的:
tom::string TXT;
TXT="abcde" + "1234";
cout << TXT << endl;
和输出必须是:
abcde1234
abcde1234
我想以后添加更多不只是const char *的东西,
i want to add more than just const char* later like:
..
int NUM=34;
TXT="abcd"+NUM+"098";
cout << TXT << endl;
和输出必须是:
abcd34098
abcd34098
我已经用运算符<<
ive already done similar thing with operator <<
TXT << "abcd" << ".......";
但是我需要使用+运算符. 另一件事是(可能将使用+运算符进行排序)
but i need it with + operator. another thing is (probably it will be sorted with + operator)
void testF(tom::string INP) {
cout << INP << endl;
}
int NUM=123;
testF("abcd"+NUM+"efg");
输出:
abcd123efg
abcd123efg
如果我仍然尝试以错误结尾的任何事情:
if i'm trying anything still ending with error:
错误:类型无效的操作数 ‘const char [4]’和‘const char [3]’ 转换为二进制"operator +"
error: invalid operands of types ‘const char [4]’ and ‘const char [3]’ to binary ‘operator+’
这是tom :: string类的一部分:
here is part of the tom::string class:
namespace tom {
class string {
private:
unsigned int _length;
unsigned int _search_pos;
bool _changed;
bool _indexed;
char* _buffer;
unsigned int* _indexes;
unsigned int _indexCount;
char* _emptyChar;
unsigned int _null;
char* _retBuffer[RET_BUFFERS];
short unsigned int _retBufferIndex;
// ADD to string
void _add (const char* txt) {
_buffer=(char*) realloc(_buffer, sizeof(char)*(_length+strlen(txt)+1));
memcpy(&_buffer[_length], txt, strlen(txt));
_length=_length+strlen(txt);
_buffer[_length]=static_cast<char>(0);
_changed=true;
free(_indexes);
_changed=true;
_indexCount=0;
_indexed=false;
_indexes = (unsigned int*) malloc (sizeof(unsigned int)*2);
}
// .......
// REPLACE Whole string
string& _rvs(const char* txt) {
free(_buffer);
free(_indexes);
_changed=true;
_indexCount=0;
_indexed=false;
_indexes = (unsigned int*) malloc (sizeof(unsigned int)*2);
_length=strlen(txt);
_buffer = (char*) malloc (sizeof(char)*(_length+1));
memcpy(_buffer, txt, _length);
_buffer[_length]=static_cast<char>(0);
return (*this);
}
// .......
public:
// ----------------------------------------------
// | CONSTRUCTOR |
// ----------------------------------------------
string(const char* _init="") {
_length=0;
_indexCount=0;
_changed=false;
_indexed=false;
_buffer = (char*) malloc (sizeof(char)*(strlen(_init)+1));
memcpy(_buffer, _init, strlen(_init));
_indexes = (unsigned int*) malloc (sizeof(unsigned int)*2);
_emptyChar = (char*) malloc (sizeof(char));
_buffer[strlen(_init)]=static_cast<char>(0);
_emptyChar[0]=static_cast<char>(0);
_null=(unsigned int)-1;
_retBufferIndex=0;
for (short unsigned int ii=0; ii<RET_BUFFERS; ii++) {
_retBuffer[ii] = (char*) malloc (sizeof(char));
_retBuffer[ii][0]=static_cast<char>(0);
}
}
string(const tom::string& _init) {
string((const char*)_init.c_str());
}
// ----------------------------------------------
// | DESTRUCTOR |
// ----------------------------------------------
~string() {
free(_buffer);
free(_indexes);
free(_emptyChar);
for (short unsigned int ii=0; ii<RET_BUFFERS; ii++) {
free(_retBuffer[ii]);
}
}
// .....
string& operator = (string &ttxt) {
const char* txt=ttxt.c_str();
return (_rvs(txt));
}
string& operator = (const char* txt) {
return (_rvs(txt));
}
string& operator = (int num) {
char bf[32];
sprintf (bf, "%d", num);
const char* txt=bf;
return (_rvs(txt));
}
string& operator << (const char* txt) {
_add(txt);
return(*this);
}
string& operator << (int num) {
char bf[32];
sprintf (bf, "%d", num);
const char* txt=bf;
_add(txt);
return(*this);
}
operator const char*() {
return (const char*)_buffer;
}
// .....
}
}
推荐答案
tom::string TXT;
TXT="abcde" + "1234";
cout << TXT << endl;
"abcde" + "1234"
首先被评估-您无法使其如期望的那样工作.
"abcde" + "1234"
is evaluated first - you cannot make it work like you want.
您可以例如这项工作,但:
You can make e.g. this work though:
tom::string TXT;
TXT=tom::string("abcde") + 987 + "1234";
cout << TXT << endl;
这将需要operator+(int)
和operator+(char const *)
样本运算符:
operator +应该返回一个新对象-请勿修改调用它的对象.
operator+ should return a new object - not modify the object it is called on.
class string {
...
friend string operator+(string const & LHS, char const * RHS) {
string s = LHS;
s._add(RHS);
return s;
}
};
这篇关于重载运算符+/char * ch1 + char * ch2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!