3 Replies - 205 Views - Last Post: 19 March 2012 - 08:42 PM Rate Topic: -----

#1 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

typedef question

Posted 19 March 2012 - 08:13 PM

I have been given an assignment to extend my shipping program to be able to store objects in a vector, which is fine, I got it figured out and the code runs just fine, but I have a question on why I needed to use typedef to get this completed.

Here is a snippet of the used typedef in question:
typedef vector <twoDay *> TD;
TD two;
typedef vector <overnight *> ON;
ON over;

/* Some more code */
/* Create a new object pointer of the twoDay class and push it into the two vector */
twoDay *tdPtr = new twoDay(sFirstName, sLastName, rFirstName, rLastName,
						   sAddress, rAddress, sCity, rCity, sState, rState,
						   sZip, rZip, weightInOunces, costPerOunce, flatFee);
two.push_back(tdPtr);
		
/* Creat a new object pointer of the overnight class and push it into the over vector */
overnight *onPtr = new overnight(sFirstName, sLastName, rFirstName, rLastName,
							  sAddress, rAddress, sCity, rCity, sState, rState,
							  sZip, rZip, weightInOunces, costPerOunce, overnightFee);
over.push_back(onPtr);


This post has been edited by IngeniousHax: 19 March 2012 - 08:14 PM


Is This A Good Question/Topic? 0
  • +

Replies To: typedef question

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4154
  • View blog
  • Posts: 12,941
  • Joined: 25-December 09

Re: typedef question

Posted 19 March 2012 - 08:23 PM

If you are using a vector, why are you using pointers? The whole point of vectors is to relieve you from dynamically allocating memory to store your data. Something more like:
std::vector<twoDay> two;
two.push_back(twoDay(sFirstName, sLastName, rFirstName, rLastName,
		     sAddress, rAddress, sCity, rCity, sState, rState,
	             sZip, rZip, weightInOunces, costPerOunce, flatFee));


Jim
Was This Post Helpful? 1
  • +
  • -

#3 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: typedef question

Posted 19 March 2012 - 08:33 PM

Fair enough, that works as well. What is typedef keyword used for than? Is there some kind of documentation I could read up on or is there a quick kind of explanation?
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4154
  • View blog
  • Posts: 12,941
  • Joined: 25-December 09

Re: typedef question

Posted 19 March 2012 - 08:42 PM

A typedef is usually used to make a complicated expression smaller. For example:
typedef std::vector<std::string> Vstring; 

Vstring yourString;
// same as
//std::vector<std::string> youString;



However it is considered bad practice to hide a pointer inside a typedef. Don't do this:
typedef int* myIntPointer;



See this link: typedef

Jim
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1