Returning objects from member functions

How to return an object from a class member function?

Page 1 of 1

2 Replies - 1319 Views - Last Post: 24 March 2009 - 02:00 PM Rate Topic: -----

#1 hk416  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 11-October 08

Returning objects from member functions

Post icon  Posted 23 March 2009 - 02:57 PM

I have a problem I'm not sure on how to solve regarding objects and how to return them from member functions. In my class "TicketManagement" I have the following member function:
//Takes index as argument. Function should return the reference to object at index. (Rowrevenues is a Vector with objects).
//Returns the reference to the object stored at index (object is a specific purchace with 3 member variables).
const TicketManagment TicketManagment::getAllRevenues(int &index) const {
	if (index >= 0 && index < Rowrevenues.size())														
		return &Rowrevenues.at(index);
	else
		return 0;
	}



In a function in my main() file I have the following code to get at the specific member variables via their get-functions:
//Loop wich writes all contents to file
	for (int index = 0; index < manage.getRowRevenuesSize(); index++){	
		const TicketManagment purchase = manage.getAllRevenues(index);
		
		DataToFile = purchase->getSeatRow() + (";") + purchase->getPriceTotal() + (";") + purchase->getNumberofTickets();
//Content is written to a CSV-type file		
transfer.WriteToFile(FILEPATH_REVENUES, DataToFile, false);
		} 



I'm really not sure on what the proper syntax is in order to get this to work. I've been at it a while and I've run out of luck...

Is This A Good Question/Topic? 0
  • +

Replies To: Returning objects from member functions

#2 jbo5112  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 23-March 09

Re: Returning objects from member functions

Posted 23 March 2009 - 03:34 PM

A couple of bugs (maybe more), but this should at least be really close. I've just marked up your code with comments and inserted corrected code

//Takes index as argument. Function should return the reference to object at index. (Rowrevenues is a Vector with objects).
//Returns the reference to the object stored at index (object is a specific purchace with 3 member variables).
//
// The Following line returns a copy of the TicketManagment object
// const TicketManagment TicketManagment::getAllRevenues(int &index) const {
//
// This is will actually return a reference and ensure your index variable isn't modified.
// I'm not sure how this will work though.  The TicketManagment object looks like it has a vector
// of TicketManagment object.  If the vector is static, it might work.
const TicketManagment &TicketManagment::getAllRevenues(const int &index) const {
	if (index >= 0 && index < Rowrevenues.size())														
		//This code returns a pointer to whatever is there.
		//return &Rowrevenues.at(index);

		// This will return the object there
		return Rowrevenues.at(index);
	else
		// This will cause a problem if 0 isn't a TicketManagment object
		// The best I can think of returning is an unmodified TicketManagment object,
		// and make sure to declare it static so there is one default TicketManagment object
		// for the whole class and not a TicketManagment object per TicketManagment object
		return 0;
	}



	//Loop wich writes all contents to file
	for (int index = 0; index < manage.getRowRevenuesSize(); index++){	
		// This code creates a const copy of the object returned
		// const TicketManagment purchase = manage.getAllRevenues(index);

		// This creates a const reference to the object and uses less overhead
		const TicketManagment &purchase = manage.getAllRevenues(index);

		// This code is for purchase being a pointer, not a reference or copy of the object
		// DataToFile = purchase->getSeatRow() + (";") + purchase->getPriceTotal() + (";") + purchase->getNumberofTickets();

		// This code is for purchase being a reference, but it will probably fail if any of the functions return numbers
		// If any of the functions return numbers, then look into a strstream, stringstream, s(n)printf, or boost::lexical_cast
		// Or have a function that will take numbers and write them to a file
		DataToFile = purchase.getSeatRow() + (";") + purchase.getPriceTotal() + (";") + purchase.getNumberofTickets();

		//Content is written to a CSV-type file		
		transfer.WriteToFile(FILEPATH_REVENUES, DataToFile, false);
	} 



P.S. Management has an 'e' after the 'g'.
Was This Post Helpful? 0
  • +
  • -

#3 hk416  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 11-October 08

Re: Returning objects from member functions

Posted 24 March 2009 - 02:00 PM

Your suggestion worked fine! As the three member variables are integers, I had to create a function to convert from Integer to Strings like this:
//Converts integer contents to string via stringstream then returns the new string-value
string DataValidation::ConvertIntToString(int &_inputInteger) {
	tempInt.clear();
	stringvalue.clear();
	tempInt << _inputInteger;
	stringvalue = tempInt.str();
	return stringvalue; 
}



The function to access the member variables via their get()-functions now look like this:
/*This function accepts class objects as arguments and calls subfunctions to 
let the user perform the following task:
1) Save all data to file when the program quits*/
void SaveAllData(DataValidation &control, DisplayMessages &showMsg, FileHandling &transfer, TicketManagment &manage){
	//Init local variables and information textstrings
	int indeks = 0;
	int pricetotalConvert = 0, seatrowConvert = 0, ticketsConvert = 0;
	string DataToFile = "", pricetotal = "", seatrow = "", tickets = "";
	
	//Message to user
	cout << " Saving... " << endl;

	//Loop wich writes all contents to file
	for (int index = 0; index < manage.getRowRevenuesSize(); index++){	
		const TicketManagment &purchase = manage.getAllRevenues(index);
		
		//convert pricetotal from int to string
		pricetotalConvert = purchase.getPriceTotal();
		pricetotal = control.ConvertIntToString(pricetotalConvert);
		
		//convert seatrow from int to string
		seatrowConvert = purchase.getSeatRow();
		seatrow = control.ConvertIntToString(seatrowConvert);
		
		//convert tickets from int to string
		ticketsConvert = purchase.getNumberofTickets();
		tickets = control.ConvertIntToString(ticketsConvert);
		
		//create output string to be written to file. Content is written to a CSV-type file
		DataToFile = seatrow + (";") + pricetotal + (";") + tickets;
		transfer.WriteToFile(FILEPATH_REVENUES, DataToFile, false);
		} 
	}



And of course as one solves one problem, another one appears. If I enter the following values for the member variables in my program:
PriceTotal: 70
SeatRow:  2
Tickets:	1 



then this is the output which is written to file:
702;70;7021



The pricetotal string contains the correct value (70), while the seatrow string seems to contain the values of both seatrow and pricetotal (702). The tickets string contains the values of all the variables (7021) !?
I've tried resetting all the variables before each loop iteration but that changes nothing. However re-arranging the sequence of these three sentences changes things. Basicly the first variable contains it's own value. The second variable has it's own value and the value of the variable above it, while the third variable has its own value and the values of the variables above it. I have no idea why this is happening... or how it is happening...
	seatrow = control.ConvertIntToString(seatrowConvert);
	tickets = control.ConvertIntToString(ticketsConvert);
	pricetotal = control.ConvertIntToString(pricetotalConvert);




If I try to write for example 5 random objects to file i end up with this mess:
2704;270;27043
270434003;27043400;2704340035
27043400352809;2704340035280;270434003528092
270434003528092114013;2704340035280921140;2704340035280921140136
27043400352809211401363306;2704340035280921140136330;270434003528092114013633063




Any idea on what is wrong with the loop?

This post has been edited by hk416: 24 March 2009 - 02:36 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1