pointer to member?

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

Join Date: Jul 2004
Posts: 1
Reputation: ihvc4 is an unknown quantity at this point 
Solved Threads: 0
ihvc4 ihvc4 is offline Offline
Newbie Poster

pointer to member?

 
0
  #1
Jul 13th, 2004
I have the 10 classes car and a member of this class is the integer weight
how can i create an aray of the ineger weight with ponters to that member? car
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 1,620
Reputation: kc0arf is a jewel in the rough kc0arf is a jewel in the rough kc0arf is a jewel in the rough 
Solved Threads: 51
Team Colleague
kc0arf kc0arf is offline Offline
Posting Virtuoso

Re: pointer to member?

 
0
  #2
Jul 13th, 2004
Hello,

If I understand you correctly, you have 10 categories (called cars), and you want to collect data on the car.

You can use a struct to handle this, or make a simple standard that says the 1 storage unit will be the 1 car class, the second storage unit the second class... and go from there.

My guess is that you are going to want to store more than the car's weight.

Make a struct

struct data
{
string carname;
int weight;
}

And then you can make an array:

struct data CarData[10];

And then you can refrence them as you need them:

CarData[3].weight= the value of the weight of the third car.

Give it a shot, and post your code to see if you have it all.

Christian
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 13
Reputation: glSuccinct is an unknown quantity at this point 
Solved Threads: 2
glSuccinct's Avatar
glSuccinct glSuccinct is offline Offline
Newbie Poster

Re: pointer to member?

 
1
  #3
Aug 12th, 2004
Off the top of me head:
  1. #include <iostream>
  2. #include <conio.h>
  3.  
  4. using std::cout;
  5. using std::endl;
  6.  
  7. struct TData
  8. {
  9. float fWeight;
  10. float fLength;
  11. };
  12.  
  13. typedef float (TData::*TDataFloatPointer); // pointer to member
  14.  
  15. float CalcAverage(const TData* pArray, int iSize, TDataFloatPointer p)
  16. {
  17. float fAverage = 0;
  18. for(int i = 0; i < iSize; ++i)
  19. {
  20. float f = pArray->*p; // derefernce through the pointer to member
  21. fAverage += f;
  22. ++pArray;
  23. }
  24.  
  25. return fAverage/iSize;
  26. }
  27.  
  28. void PrintArrayData(const TData* pArray, int iSize, TDataFloatPointer p)
  29. {
  30. float fAverage = CalcAverage(pArray, iSize, p);
  31.  
  32. for(int i = 0; i < iSize; ++i)
  33. cout << pArray++->*p << endl;
  34.  
  35. cout << "Average:" << endl << fAverage << endl;
  36. }
  37.  
  38. int main()
  39. {
  40. const TData vData[] =
  41. {
  42. { 1.0f, 10.0f },
  43. { 2, 9 },
  44. { 3, 8 },
  45. { 4, 7 },
  46. { 5, 6 },
  47. { 6, 5 },
  48. { 7, 4 },
  49. { 8, 3 },
  50. { 9, 2 },
  51. { 10, 1 }
  52. };
  53. const NUM_DATA = sizeof(vData)/sizeof(vData[0]);
  54.  
  55. cout << "Weights:" << endl;
  56. PrintArrayData(vData, NUM_DATA, &TData::fWeight); // get the pointer to member
  57.  
  58. cout << "Length:" << endl;
  59. PrintArrayData(vData, NUM_DATA, &TData::fLength);
  60.  
  61. getch();
  62. }
Using a pointer to member construct, this will collect each member's weight or length and return an average.

Pointers to members are a niche type of programing mechanism. There are very few instances where pointers to members are even useful, but the times they are, they're invaluable. They're most useful, in my experince, in certain forms of automation, such as running the same algorithm over different members of the same type. Thus particular example could be boiled down to about 10 lines of code were I to go further w/ the pointers to members and templates, but this is a much more clear example.

Hope it helps!
-don't listen to me-
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 13
Reputation: glSuccinct is an unknown quantity at this point 
Solved Threads: 2
glSuccinct's Avatar
glSuccinct glSuccinct is offline Offline
Newbie Poster

Re: pointer to member?

 
0
  #4
Aug 12th, 2004
Ooh, forgot to mention.

In 9 out for 10 times, if you know what types of data you're going to be working on, it's easier just to do like kc0arf said, and just dereference manually.

The only reasons for pointers to members are when you cannot guarantee that you know anything about the source objects (i.e. templates: typedef T1 (TBase:TMemberPointer), or you're running an algo on many parts of it.
-don't listen to me-
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC