| | |
pointer to member?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2004
Posts: 1,620
Reputation:
Solved Threads: 51
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
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
Off the top of me head:
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!
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <conio.h> using std::cout; using std::endl; struct TData { float fWeight; float fLength; }; typedef float (TData::*TDataFloatPointer); // pointer to member float CalcAverage(const TData* pArray, int iSize, TDataFloatPointer p) { float fAverage = 0; for(int i = 0; i < iSize; ++i) { float f = pArray->*p; // derefernce through the pointer to member fAverage += f; ++pArray; } return fAverage/iSize; } void PrintArrayData(const TData* pArray, int iSize, TDataFloatPointer p) { float fAverage = CalcAverage(pArray, iSize, p); for(int i = 0; i < iSize; ++i) cout << pArray++->*p << endl; cout << "Average:" << endl << fAverage << endl; } int main() { const TData vData[] = { { 1.0f, 10.0f }, { 2, 9 }, { 3, 8 }, { 4, 7 }, { 5, 6 }, { 6, 5 }, { 7, 4 }, { 8, 3 }, { 9, 2 }, { 10, 1 } }; const NUM_DATA = sizeof(vData)/sizeof(vData[0]); cout << "Weights:" << endl; PrintArrayData(vData, NUM_DATA, &TData::fWeight); // get the pointer to member cout << "Length:" << endl; PrintArrayData(vData, NUM_DATA, &TData::fLength); getch(); }
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-
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.
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-
![]() |
Similar Threads
- pointer to member of struct (C)
- what's the difference between using an ordinary pointer and a "pointer to member"? (C++)
- Pointer to Member Function (C++)
- Passing a member function pointer (C++)
- Pointer to a data member (C++)
Other Threads in the C++ Forum
- Previous Thread: Selecting random word from file (C++)
- Next Thread: using(STL)function object (bind2nd) with a user defined function object
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template templates test text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






