Combined data type arrays.

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

Join Date: Mar 2006
Posts: 13
Reputation: Podge is an unknown quantity at this point 
Solved Threads: 0
Podge's Avatar
Podge Podge is offline Offline
Newbie Poster

Combined data type arrays.

 
0
  #1
Mar 24th, 2006
VC++ 8

I have created a form class with a combo box. I want to populate the combo box with data calculated from the form. This I can do. But I want to tie the string data in the combo box to numeric (double) value. Select the string and get a value. The information needs to be static.

Where I am having a problem is how to create a string and a double in an array. I tried several approaches with no luck. I do not want some one to write code for me. I want to understand how to create cobined data type arrays that can be sorted.

If I create a class for storing data, can I use a vector STL to crate an array of that object? Something like:


ref class MyStorage
{
public: MyStorage::MyStorage(void)
{
}
public: String^ MyString;
public: double MyDecimal;
public: int MyInt;
};

From my form class can I do something like this?

typedef vector< MyStorage > MY__OBJ;
typedef MY_OBJ::iterator MY_OBJ_IT;


Is this concept flawed?


Thanks,
Todd
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,580
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Combined data type arrays.

 
0
  #2
Mar 24th, 2006
>Is this concept flawed?
No, that's a good way to do it. Create a class that wraps the types you want and supports sorting on the fields you want, then create a container of objects of that class.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,671
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Combined data type arrays.

 
0
  #3
Mar 24th, 2006
The good news is that you can write your class much cleaner like this:
  1. class MyStorage
  2. {
  3. public:
  4. MyStorage()
  5. {
  6. }
  7. string MyString;
  8. double MyDecimal;
  9. int MyInt;
  10. };
All member variables of the MyStorage class still have public access.

The :: operator should be used if you define the method/constructor outside the body of the class declaration. In fact, if you aren't going to initialize variables to a default value, use dynamic memory for variables, etc., then you don't even need to explicitly declare a default constructor, the compiler will do it for you, just like it will the assignment operator, copy constructor and destructor if you don't explicitly declare and define them. Knowing when to declare and define your own version of the default constructor, copy constructor, assignment operator and destructor explicitly and when to accept the versions supplied by the compiler by default may take a little practice.
  1. class MyStorage
  2. {
  3. public:
  4. string MyString;
  5. double MyDecimal;
  6. int MyInt;
  7. };

And because in C++ structs are the same as classes with member variables having public access by default you even simplify to this:
  1. struct MyStorage
  2. {
  3. string MyString;
  4. double MyDecimal;
  5. int MyInt;
  6. };

Cool huh?
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 13
Reputation: Podge is an unknown quantity at this point 
Solved Threads: 0
Podge's Avatar
Podge Podge is offline Offline
Newbie Poster

Re: Combined data type arrays.

 
0
  #4
Mar 24th, 2006
Real cool! Thanks,

if you aren't going to initialize variables to a default value, use dynamic memory for variables, etc.,
Can I initialize from inside another class using gcnew? I am struggling with setting up a vector array. I am missing something. Is this the correct way to do this?

typedef vector< MyStorage^ > MY_OBJ;

I can’t seem to get the concept.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,671
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Combined data type arrays.

 
0
  #5
Mar 25th, 2006
I think this will work. Haven't compiled/run to prove it though.
  1. #include <iostream>
  2. #incldue <string>
  3. #include <vector>
  4.  
  5. using namespace std;
  6. struct MyStorage
  7. {
  8. //default constructor
  9. MyStorage(): MyString(""), MyDecimal(0.0), MyInt(0){}
  10.  
  11. //three parameter constructor
  12. MyStorage(string s, double d, int i) : MyString(s), MyDecimal(d), MyInt(i) {}
  13.  
  14. //copy constructor
  15. MyStorage(const MyStorage & m) : MyString(m.MyString),
  16. MyDecimal(m.MyDecimal),
  17. MyInt(m.MyInt)
  18. {}
  19.  
  20. //variables
  21. string MyString;
  22. double MyDecimal;
  23. int MyInt;
  24.  
  25. //overloaded << operator to simplify printing contents of MyStorage objects
  26. friend ostream & operator<<(ostream & os, const MyStorage & m)
  27. {
  28. os << m.MyString << ' ' << m.MyDecimal << ' ' << m.MyInt;
  29. return os;
  30. }
  31. };
  32.  
  33. int main()
  34. {
  35. vector<MyStorage> myVector;
  36. string str = "trial";
  37. int i;
  38. const int MAX = 3;
  39. for(i = 0; i < MAX; ++i)
  40. myVector.push_back(MyStorage temp(str, i * 1.23, i ));
  41.  
  42. vector<MyStorage>::iterator start = myVector.begin();
  43. vector<MyStorage>::iterator stop = myVector.end();
  44. for( ; start != stop; ++start)
  45. cout << *start << endl;
  46. }

>>Can I initialize from inside another class using gcnew?

I am not sure what you mean by this question. You can certainly declare a MyStorage object from within another class using either static memory or dynamic memory (the new operator) as long as the MyStorage class has been declared before you declare MyStorage object. Is that your intent, or something else?
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