question about operator overloading

This is a discussion on question about operator overloading within the C++ Programming forums, part of the General Programming Boards category; hello everyone! I met some problems with pointer here: Code: //class's declaration //file FunnyString.h #ifndef FUNNYSTRING_H #define FUNNYSTRING_H #include <cassert> ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    1

    Unhappy question about operator overloading

    hello everyone!
    I met some problems with pointer here:

    Code:
    //class's declaration 
    //file FunnyString.h
    #ifndef FUNNYSTRING_H
    #define FUNNYSTRING_H
    #include <cassert>
    #include <iostream>
    
    using namespace std;
    class FunnyString
    {
    	friend istream &operator>>( istream&, FunnyString& );
    	friend ostream &operator<<( ostream&, FunnyString&) ;
    public:
    	FunnyString(char* = 0);
    	~FunnyString()
    	{
    		delete [] data;
    	}
    	FunnyString( FunnyString& ); //copy constructor.
    	FunnyString  operator+( FunnyString& );
    	FunnyString & operator=(  FunnyString& ); //assigment operator.
    private:
    	void SetString( char*);
    	char GetOneCharacter( const int & index )
    	{
    		assert( ( index < len ) && index>= 0 );
    		return data[index];
    	}
    	void CutBeginCharacter();
    	void SelfCutEndCharacter();
    	char* data;
    	int len;
    };
    #endif
    Code:
    //FunnyString class's definition.
    //FunnyString.cpp file.
    #include "FunnyString.h"
    #include <iostream>
    #include <cassert>
    using namespace std;
    
    FunnyString::FunnyString(char *s )
    :len(strlen(s)+1)
    {
    	SetString(s);
    }
    FunnyString::FunnyString( FunnyString & s)
    {
    	SetString(s.data);
    }
    FunnyString& FunnyString::operator= (  FunnyString& s)
    {
    	if ( &s != this ) //prevent self assigment.
    	{
    		if ( s.len != len )
    		{
    			delete [] data;
    			SetString(s.data);
    		}
    	}
    	else cout << "Self assigment\n";
    	return *this;
    }
    FunnyString  FunnyString::operator+( FunnyString & s)
    {
    	FunnyString fString1(data);
    	FunnyString fString2(s.data);
            FunnyString &tmp = fString1;
    	while ( fString1.data[len-2] == fString2.data[0] )
    	{
    		fString1.SelfCutEndCharacter();
    		fString2.CutBeginCharacter();
    	}
    	strcat(fString1.data,fString2.data);
    	return tmp;
    }
    ostream& operator<< (ostream& output,FunnyString& s )
    {
    	output << s.data;
    	return output;
    }
    istream& operator>> (istream& input, FunnyString& s)
    {
    	char tmp[100];
    	input >> tmp;
    	s.data = tmp;
    	return input;
    }
    void FunnyString::SetString( char* s )
    {
    	if ( 0 != s )
    	{
    		len = strlen(s)+1;
    		data = new char[len];
    		strcpy(data,s);
    	}
    	else 
    	{
    		len = 1;
    		data = 0;
    	}
    }
    void FunnyString::CutBeginCharacter()
    {
    	char* tmp = new char[len-1];
    	for ( int i = 0; i < len - 2; i++)
    	{
    		tmp[i]=data[i+1];
    	}
    	tmp[len-2] = 0;
    	delete [] data;
    	SetString(tmp);
    	delete [] tmp;
    }
    void FunnyString::SelfCutEndCharacter()
    {
    	char* tmp = new char[len-1];
    	strncpy(tmp,data,len-2);
    	tmp[len-2] = 0;
    	delete [] data;
    	SetString(tmp);
    	delete [] tmp;	//release tmp;
    }
    The operator+ function met a pointer problem(I don't know why, I just want this function does not change the data, so I created 2 new FunnyString objects and handle them. This function return a reference of FunnyString!!!- maybe when the function end, all the data will be destruct by destructor which released the char* data, so function can not return it).But I can't prevent it. this is a operator overloading, it can not contain more parameter,so how can I return in this function?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    This string is funny indeed. Couldn't operator+ allocate enough space in a string object and then copy both strings to that buffer?

    There seems to be an insane amount of memory allocations and deallocations going on. The cut functions in particular seem rather useless and the logic behind string concatenation is completely unclear.

    There are also const correctness issues.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 03:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 06:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 01:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 10:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21