Thread: Weird Problem with return with operator +

  1. #1
    Registered User KonArtis's Avatar
    Join Date
    Mar 2003
    Posts
    34

    Question Weird Problem with return with operator +

    Code:
    class cXLONG
    {
    private:
    	char*	pszNumber;
    	int	iLen;
    public:
    	cXLONG() :pszNumber(0), iLen(0)			{	}
    	cXLONG(int iNumber)	:pszNumber(0)		{ *this=iNumber; }
    	cXLONG(const char* pszNumber) :pszNumber(0)	{ *this=pszNumber; }
    	~cXLONG()					{ if ( this->pszNumber ) delete[] this->pszNumber; }
    
    	const char* c_str() const			{ return this->pszNumber; }
    
    	void operator=(int iNumber);
    	void operator=(const char* pszNumber);
    	void operator=(const cXLONG& xlNumber);
    
    	cXLONG operator+(int iNumber);
    	cXLONG operator+(const char* pszNumber);
    	cXLONG operator+(const cXLONG& xlNumber);
    
    };
    
    cXLONG cXLONG::operator+(const char* pszNumber)
    {
    	cXLONG	xlReturn;
    	
    	//some code
    
    	xlReturn=pszTmp;
    	return xlReturn;
    }
    When ever the operator + is called the return value is junk. What is the problem.

    Thanks.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    What in the world is pszTmp?

    gg

  3. #3
    Registered User KonArtis's Avatar
    Join Date
    Mar 2003
    Posts
    34
    Sorry. pszTmp is a string. Theres no problem with that statement. Its the next one;
    return xlReturn
    It calls the decontructor before is constructs the return for some reason. I've written other classes/functions that return a class like I'm doing here. But it doesn't work in this case for some reason.

    Thanks.

  4. #4
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    I don't know what the problem is, but operator+ should return a constant object.
    That is to prevent statements like:
    Code:
    (a+1) = 3;
    EDIT: Jawib is correct, too.
    Last edited by Sang-drax; 05-05-2004 at 10:43 AM.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Shouldn't your operator= return a reference to a cXLONG object?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    Registered User KonArtis's Avatar
    Join Date
    Mar 2003
    Posts
    34
    Figured it out. It was a very stupid mistake, I forgot the copy constructor.
    Last edited by KonArtis; 05-05-2004 at 03:53 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New string functions
    By Elysia in forum C Programming
    Replies: 11
    Last Post: 03-28-2009, 05:03 AM
  2. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 02:39 PM
  3. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM
  4. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  5. Can't figure out problem with code
    By Beast() in forum C Programming
    Replies: 4
    Last Post: 04-16-2005, 05:27 PM