function style cast question

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

function style cast question

 
0
  #1
Feb 14th, 2007
I'm having a bit of a problem with function style cast with classes.

When I exit the function I expect an instance of Database to be returned. This instance would then be copied into the db declared in main. I've writtien a copy constructor and it works fine (tested by ordinary initialization) but what I can't seem to copy the data over.

  1. // main
  2. Database const db = makeDb();
  3.  
  4. // makeDb()
  5. Database makeDb()
  6. {
  7. int const size = 100;
  8. char file[size];
  9.  
  10. cout << "Enter a file name <press ENTER to use the default name>: ";
  11. cin.getline( file, size );
  12.  
  13. // Database db(file);
  14.  
  15. // db.fillDatabase();
  16. // db.printDatabase();
  17.  
  18. return Database(file);
  19. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

Re: function style cast question

 
0
  #2
Feb 15th, 2007
erg i take it back. The problem i'm actually having is with trying to return something by a method called 'return value optimization.' I can't seem to create a copy of the db in the function to the one in in main.
Last edited by degamer106; Feb 15th, 2007 at 12:09 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1461
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: function style cast question

 
0
  #3
Feb 15th, 2007
If you don't post your code there is nothing anyone can do for you, except maybe give you a little sympathy.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

Re: function style cast question

 
0
  #4
Feb 15th, 2007
sry double post
Last edited by degamer106; Feb 15th, 2007 at 1:17 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

Re: function style cast question

 
0
  #5
Feb 15th, 2007
  1. #include <iostream>
  2. #include <cstring>
  3. #include <fstream>
  4.  
  5. #include "state.h"
  6. #include "database.h"
  7.  
  8. using std::cout;
  9. using std::cin;
  10. using std::ifstream;
  11.  
  12. class Format
  13. {
  14. public:
  15. Format(char * string);
  16. ~Format() { delete [] it; }
  17. char * getString() { return it; }
  18. void fixString();
  19. void upperCase();
  20. void print() { cout << it << '\n'; }
  21. private:
  22. char * it;
  23. int length;
  24. };
  25.  
  26. Database makeDb();
  27. ////////////////////////// program start ///////////////////////////////////////
  28. int main(void)
  29. {
  30. const Database db = makeDb();
  31.  
  32. int const size = 100;
  33. char stateName[size];
  34.  
  35. cout << "\nEnter a state name: ";
  36. cin.getline(stateName, size);
  37.  
  38. Format string(stateName);
  39.  
  40. string.fixString();
  41. // string.print();
  42. const State foundState = db.getState( string.getString() );
  43.  
  44. return 0;
  45. }
  46.  
  47. int State::findState(char * stateName, State & foundState)
  48. {
  49. if ( strcmp( stateName, name ) == 0 )
  50. {
  51. foundState.name = new char[strlen(name) + 1];
  52. strcpy(foundState.name, name);
  53.  
  54. foundState.year = year;
  55.  
  56. foundState.cap = new char[strlen(cap) + 1];
  57. strcpy(foundState.cap, cap);
  58.  
  59. foundState.popRank = popRank;
  60.  
  61. return 1;
  62. }
  63. else
  64. return 0;
  65. }
  66.  
  67. State Database::getState(char * stateName) const
  68. {
  69. State foundState;
  70.  
  71. for (int cnt = 0, found = 0; cnt < count && !found; cnt++)
  72. found = list[cnt].findState( stateName, foundState );
  73.  
  74. if ( found )
  75. cout << stateName << " was found!\n";
  76. else
  77. cout << stateName << " is not in the database!\n";
  78.  
  79. return foundState;
  80. }
  81.  
  82. Format::Format(char * string)
  83. {
  84. length = strlen(string) + 1;
  85. it = new char[length];
  86. strcpy( it, string );
  87. }
  88.  
  89. void Format::fixString()
  90. {
  91. // capitalize the first letter of the sentence
  92. it[0] = toupper( it[0] );
  93.  
  94. for (int cnt = 1; cnt < length; cnt++)
  95. if ( isalpha( it[cnt] ) )
  96. it[cnt] = tolower( it[cnt] ); // set to lower case
  97. else if ( it[cnt] == ' ' )
  98. {
  99. ++cnt; // move to the beginning of the next word
  100. it[cnt] = toupper( it[cnt] ); // capitalize
  101. }
  102. }
  103.  
  104. void Format::upperCase()
  105. {
  106. for (int cnt = 0; cnt < length; cnt++)
  107. if ( isalpha( it[cnt] ) )
  108. it[cnt] = toupper( it[cnt] );
  109. }
  110.  
  111. Database makeDb()
  112. {
  113. int const size = 100;
  114. char file[size];
  115.  
  116. cout << "Enter a file name <press ENTER to use the default name>: ";
  117. cin.getline( file, size );
  118.  
  119. Database db(file);
  120.  
  121. db.fillDatabase();
  122. db.printDatabase();
  123.  
  124. return db;
  125. }
  126.  
  127. ////////////////////// Database functions ////////////////////////////////
  128. // Constructor
  129. Database::Database(char * file)
  130. {
  131. cout << "Now in the Database constructor" << '\n';
  132.  
  133. if ( !file[0] )
  134. strcpy( file, "states.txt" );
  135.  
  136. // open input file and check for open success
  137. if ( ifstream inputFile(file) )
  138. {
  139. // allocate memory for Database struct and check for success
  140. // allocate memory for filename field and initializes with filename
  141. if ( filename = new char[strlen( file ) + 1] )
  142. strcpy( filename, file );
  143. else
  144. cout << "Not enough memory!" << '\n';
  145.  
  146. // initialize count field
  147. count = 5;
  148.  
  149. // initialize list field by allocating memory for array of 50 states
  150. // and check for success
  151. if ( list = new State[count] ) {}
  152. else
  153. cout << "Not enough memory!" << '\n';
  154.  
  155. // close input file
  156. inputFile.close();
  157. }
  158. else
  159. {
  160. cout << "File not found!" << '\n';
  161. exit(0);
  162. }
  163. }
  164.  
  165. Database::Database(const Database & arg)
  166. {
  167. cout << "Now in the copy constructor\n";
  168.  
  169. if ( filename = new char[strlen( arg.filename ) + 1] )
  170. strcpy( filename, arg.filename );
  171. else
  172. cout << "Not enough memory!\n";
  173.  
  174. count = arg.count;
  175.  
  176. if ( list = new State[count])
  177. for ( int cnt = 0; cnt < count; cnt++)
  178. insertDatabase( cnt, arg.list[cnt] );
  179. else
  180. cout << "Not enough memory!\n";
  181. }
  182.  
  183. Database::~Database()
  184. {
  185. cout << "Now in the database destructor\n";
  186. delete [] list;
  187. delete [] filename;
  188. }
  189. //fillDatabase - reads info from file and stores in array of State structs
  190. //input: pointer to Database struct
  191. //return: nothing
  192. void Database::fillDatabase ()
  193. {
  194. char name[80]; // temp variables
  195. int year;
  196. char cap[80]; // input data
  197. int popRank;
  198. int index = 0; // index into array of State structs
  199.  
  200. State tempState;
  201. char array[100]; // temporary storage for file input
  202.  
  203. // open file from filename field of db
  204. // don't need to check for file open success because initDatabase already checked
  205. ifstream inputFile( filename );
  206.  
  207. // loop through all the states
  208. for ( int cnt = 0; cnt < count; cnt++ )
  209. {
  210. // read and parse one line of file into 4 input data fields
  211. if ( !inputFile.eof() )
  212. {
  213. inputFile.getline( array, 100 );
  214.  
  215. if ( sscanf( array, "%[^,], %d, %[^,], %d", name, &year, cap,
  216. &popRank ) == 4 )
  217. {
  218. // if tempState is created successfully from the 4 input data
  219. tempState.initState(name, year, cap, popRank);
  220. insertDatabase (index, tempState);
  221. }
  222. else
  223. {
  224. cout << "Error with file!" << '\n';
  225. return;
  226. }
  227. index++;
  228. }
  229. }
  230.  
  231. // close file
  232. inputFile.close();
  233. }
  234.  
  235. void Database::insertDatabase (int index, State const & tempState)
  236. {
  237. State * pCurrent = list + index;
  238. pCurrent->copyState( tempState );
  239. }
  240.  
  241. //printDatabase - print data of state array
  242. //input: pointer to Database struct
  243. //return: nothing
  244. void Database::printDatabase (bool flag) const
  245. {
  246. cout << " State name Year Capital Pop. rank\n";
  247. cout << "------------------------------------------------\n";
  248.  
  249. if ( flag == true )
  250. for (int i = 0; i < count; i++)
  251. list[i].printState();
  252. else
  253. cout << "No data to display!" << '\n';
  254.  
  255. return;
  256. }
  257.  
  258. State::State( const State & arg )
  259. {
  260. cout << "Now in the state copy constructor\n";
  261. name = new char[strlen(arg.name) + 1];
  262. strcpy(name, arg.name);
  263.  
  264. year = arg.year;
  265.  
  266. cap = new char[strlen(arg.cap) + 1];
  267. strcpy(cap, arg.cap);
  268.  
  269. popRank = arg.popRank;
  270. }
  271.  
  272. State::~State()
  273. {
  274. cout << "Now in the state destructor\n";
  275.  
  276. cout << "deleted " << name << ',' << cap << '\n';
  277. delete [] name;
  278. delete [] cap;
  279. }
  280.  
  281. //////////////////////// State functions //////////////////////////////
  282. void State::initState(char * tempName, int tempYear, char * tempCap, int tempPopRank)
  283. {
  284. char * stateName;
  285. char * capital;
  286. // if memory allocation is successful, store name
  287. if ( stateName = new char[strlen( tempName ) + 1 ] )
  288. {
  289. strcpy( stateName, tempName );
  290. name = stateName;
  291. }
  292. else
  293. cout << "Not enough memory!" << '\n';
  294.  
  295. // store year
  296. year = tempYear;
  297.  
  298. // if memory allocation is successful, store capital
  299. if ( capital = new char[strlen( tempCap ) + 1 ] )
  300. {
  301. strcpy( capital, tempCap );
  302. cap = capital;
  303. }
  304. else
  305. cout << "Not enough memory!" << '\n';
  306.  
  307. // store population ranking
  308. popRank = tempPopRank;
  309. }
  310.  
  311. //copyState - copies contents between two State structs
  312. //input: pointer to source State struct
  313. // pointer to destination State struct
  314. //return: nothing
  315. void State::copyState (State const & tempState)
  316. {
  317. name = new char[strlen(tempState.name) + 1];
  318. strcpy ( name, tempState.name );
  319.  
  320. year = tempState.year;
  321.  
  322. cap = new char[strlen(tempState.cap) + 1];
  323. strcpy( cap, tempState.cap );
  324.  
  325. popRank = tempState.popRank;
  326. }
  327.  
  328. //printState - displays State data
  329. //input: pointer to State struct
  330. //return: nothing
  331. void State::printState ()
  332. {
  333. cout.setf(ios_base::left);
  334. cout.width(17);
  335. cout << name;
  336.  
  337. cout.setf(ios_base::left);
  338. cout.width(8);
  339. cout << year;
  340.  
  341. cout.setf(ios_base::left);
  342. cout.width(18);
  343. cout << cap;
  344.  
  345. cout.setf(ios_base::left);
  346. cout.width(2);
  347. cout << popRank << '\n';
  348.  
  349. return;
  350. }
  351.  
  352. class State
  353. {
  354. public:
  355. State() { name = 0; cap = 0; }
  356. State( const State & arg );
  357. ~State();
  358. int findState(char * stateName, State & foundState);
  359. void initState(char * tempName, int tempYear, char * tempCap, int tempPopRank);
  360. void copyState(State const & tempState);
  361. void printState();
  362. private:
  363. char * name; // state name
  364. int year; // year of entry into the union
  365. char * cap; // state capital
  366. int popRank; // population ranking
  367. };
  368.  
  369. class Database
  370. {
  371. public:
  372. Database(char * file = "states.txt");
  373. Database(const Database & arg);
  374. ~Database();
  375. State getState(char * stateName) const;
  376. void deleteDatabase ();
  377. void fillDatabase ();
  378. void insertDatabase (int index, State const & tempState);
  379. void printDatabase (bool flag = true) const;
  380. private:
  381. State * list; // array of states
  382. int count; // total count of states
  383. char * filename; // input filename
  384. };
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1461
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: function style cast question

 
0
  #6
Feb 15th, 2007
Originally Posted by degamer106 View Post
erg i take it back. The problem i'm actually having is with trying to return something by a method called 'return value optimization.' I can't seem to create a copy of the db in the function to the one in in main.
There is no such function in the code you posted. And function names can not contain spaces.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

Re: function style cast question

 
0
  #7
Feb 15th, 2007
In the makeDb() function, I returned the instance back by value. Based on my teacher's lecture last Tuesday, however, that's very wasteful (as you have to invoke ctor/dtor/copy ctor calls) and we're supposed to use return value optimization (RVO).

My original function was what I had posted originally and I could not create the db instance in main from what was returned from makeDb() via RVO. That's what I've been trying to hammer out.
Last edited by degamer106; Feb 15th, 2007 at 1:49 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1461
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: function style cast question

 
0
  #8
Feb 15th, 2007
Yes your teacher is correct -- returning the db object like you did is very wasteful of system resources and time.

What I would do is pass a reference to an existing db object to makeDb() so that it does not have to return the object.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC