Thread: With cout - Why Wont This Work?

  1. #1
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218

    With cout - Why Wont This Work?

    Heres a line of code where each function returns a pointer to a string:
    Code:
    cout << test.GetLeft(2) << test.GetMid(3, 3) << test.GetRight(2) << "\n";
    Instead of returning 3 diferent strings I get 3 copies of the first one returned.
    If I change the code to:
    Code:
    	cout << test.GetLeft(2); 
    	cout << test.GetMid(3, 3);
    	cout << test.GetRight(2) << "\n";
    [/code]
    It works. Why? And is there any way I can use all these functions in one cout statement?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well if they're all returning the same pointer to some hidden char array inside the class, then yes you'll see the effect you describe.

    > And is there any way I can use all these functions in one cout statement?
    Return a properly constructed std::string object which the compiler will dispose of for you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Show the code for the functions to find out where they went wrong.

  4. #4
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Hmm. Maybe I should have a play around with the std::string stuff and see what it can do.

    Daved: Heres the code for the functions:
    Code:
    char* String::GetLeft(Uint16 chars)
    {
    	if(chars >= size) return str_ptr;
    	if(tmp_ptr != NULL) delete []tmp_ptr;
    	tmp_ptr = new char[chars+1];	
    	char *src = str_ptr;
    	char *dst = tmp_ptr;
    	for(int i=0; i<chars; i++)
    	{
    		*dst = *src;
    		src++; dst++;
    	}
    	++*dst='\0';
    	return tmp_ptr;
    }
    char* String::GetRight(Uint16 chars)
    {
    	if(chars >= size) return str_ptr;
    	if(tmp_ptr != NULL) delete []tmp_ptr;
    	tmp_ptr = new char[chars+1];	
    	
    	char *src = str_ptr;
    	char *dst = tmp_ptr;
    	int start = size-chars-1;
    	
    	for(int i=0; i<start; i++, src++);
    	while(*src) 
    	{	
    		*dst = *src; 
    		src++; dst++;
    	}
    	++*dst='\0';
    	return tmp_ptr;
    }
    char* String::GetMid(Uint16 start, Uint16 chars)
    {
    	if(chars >= size) return str_ptr;
    	if(tmp_ptr != NULL) delete []tmp_ptr;
    	tmp_ptr = new char[chars+1];
    	
    	char *src = str_ptr;
    	char *dst = tmp_ptr;
    
    	for(int i=1; i<start; i++, src++);
    	for(int i=0; i<chars; i++, src++, dst++) *dst = *src;
    	++*dst='\0';
    	return tmp_ptr;
    }
    And heres the class definition:
    Code:
    class String
    {	
    	public:
    		String();
    		String(const char*);
    		~String(){}
    		void Add(char*);
    		void Set(char*);
    		void Print(Uint8);
    		char* Get();
    		char* GetLeft(Uint16);
    		char* GetRight(Uint16);
    		char* GetMid(Uint16, Uint16);
    		int Len();
    		int Sub(char*, bool);
    		void ToUpper();
    		void ToLower();
    		bool Replace(char*, char*, bool);
    		void ReplaceAll(char*, char*, bool);
    		bool ChopLeft(Uint16);
    		bool ChopRight(Uint16);
    		bool ChopMid(Uint16, Uint16);
    		char GetChar(Uint16);
    		void Insert(char*, Uint16);
    		void Reverse();
    		void Split(char);	//Splits string at each occurence of a character
    		void Spilt(Uint16); //Splits string at index
    	private:
    		Uint16 size;
    		char *str_ptr;
    		char *tmp_ptr;
    };

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You should be using std::string instead of your own handwritten class for general programming purposes. I assumed you were writing this as an assignment. If it isn't then why are you writing this class?

    Salem was accurate about your problem. You use a temporary character array inside the class to hold the substrings. When you output each on the same line, the functions are all called before the return values are used by cout. So each function fills the tmp_ptr array and overwrites the previous value.

    One solution is to return a String from those functions rather than a char*. If the user wants a char*, they can call Get on the result.

  6. #6
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Yeah, ok I'll try having it return a string instead. Cheers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-17-2008, 12:36 PM
  2. How properly inherit from template?
    By 6tr6tr in forum C++ Programming
    Replies: 118
    Last Post: 04-25-2008, 04:30 AM
  3. How Does cout Work
    By jrahhali in forum C++ Programming
    Replies: 8
    Last Post: 08-25-2004, 03:19 PM
  4. Why doesnt this work
    By Jotun in forum C++ Programming
    Replies: 3
    Last Post: 04-18-2004, 04:55 PM
  5. Redirecting cout stream
    By Arrow Mk84 in forum C++ Programming
    Replies: 1
    Last Post: 10-08-2002, 04:17 PM