重载运算符+ / char* ch1 + char* ch2[英] overload operator+ / char* ch1 + char* ch2

本文是小编为大家收集整理的关于重载运算符+ / char* ch1 + char* ch2的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我已经创建了具有一些额外功能的简单字符串类 - 主要用于学习目的. 现在,我想超载操作员 +让我在字符串中添加两个或更多char*.

这就是我要制作的:

tom::string TXT;
TXT="abcde" + "1234";
cout << TXT << endl;

和输出必须为:

abcde1234

我想添加不仅仅是const char*以后再添加:

..
int NUM=34;
TXT="abcd"+NUM+"098";
cout << TXT << endl;

和输出必须为:

ABCD34098

ive已经对操作员做了类似的事情<<

TXT << "abcd" << ".......";

但是我需要 +运算符. 另一件事是(可能会用 +运算符对其进行排序)

void testF(tom::string INP) {
 cout << INP << endl;
}

int NUM=123;
testF("abcd"+NUM+"efg");

输出:

ABCD123EFG

如果我尝试以错误结尾的任何东西:

错误:类型的无效操作数 " const char [4]"和" const char [3]" 到二进制"操作员+"

这是tom :: string类的一部分:

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"首先评估 - 您无法像想要的那样使其工作.

您可以做例如但是这项工作:

tom::string TXT;
TXT=tom::string("abcde") + 987 + "1234";
cout << TXT << endl;

将需要operator+(int)和operator+(char const *)

编辑:示例操作员:

操作员+应返回一个新对象 - 不修改其调用的对象.

class string {
  ...
  friend string operator+(string const & LHS, char const * RHS) {
    string s = LHS;
    s._add(RHS);
    return s;
  }
};

其他推荐答案

您不能仅用于指针类型的运算符.至少需要一种类型是用户定义的类型.

其他推荐答案

thanx to erik! ...排序(工作)

我已经添加到tom :: string class:

friend string operator+(string const & LHS, char const * RHS) {
    string s;
    s=LHS;
    s._add(RHS);
    return s;
}

下一个投掷malloc错误 - 我必须检查一下,但是第一个工作完美!

friend string operator+(string const & LHS, char const * RHS) {
    string s=LHS;
    s._add(RHS);
    return s;
}

和测试:

void test2 (tom::string ooo) {
    cout << ooo << endl;
}

test2(tom::string("abcde")+"AA"+"BB");

显示:

abcdeaabb

thanx再次!

本文地址:https://www.itbaoku.cn/post/1709364.html

问题描述

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.

this is what i want to make:

tom::string TXT;
TXT="abcde" + "1234";
cout << TXT << endl;

and output have to be:

abcde1234

i want to add more than just const char* later like:

..
int NUM=34;
TXT="abcd"+NUM+"098";
cout << TXT << endl;

and output have to be:

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");

with output:

abcd123efg

if i'm trying anything still ending with error:

error: invalid operands of types ‘const char [4]’ and ‘const char [3]’ to binary ‘operator+’

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" 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;

That will require an operator+(int) and an operator+(char const *)

EDIT: Sample 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;
  }
};

其他推荐答案

You can't overload operators for pointer types only. At least one of the involved types needs to be a user-defined type.

其他推荐答案

Thanx to Erik! ... sorted (working)

i've added to tom::string class:

friend string operator+(string const & LHS, char const * RHS) {
    string s;
    s=LHS;
    s._add(RHS);
    return s;
}

next one throwing malloc error - i have to check it, but the first working perfect!

friend string operator+(string const & LHS, char const * RHS) {
    string s=LHS;
    s._add(RHS);
    return s;
}

and the testing:

void test2 (tom::string ooo) {
    cout << ooo << endl;
}

test2(tom::string("abcde")+"AA"+"BB");

showing:

abcdeAABB

Thanx again!