开发者

overload operator+ / char* ch1 + char* ch2

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 开发者_如何学Cint)*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;
            }
// .....
    }
}


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


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


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜