944,070 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1980
  • C++ RSS
Mar 24th, 2006
0

Combined data type arrays.

Expand Post »
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
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Podge is offline Offline
13 posts
since Mar 2006
Mar 24th, 2006
0

Re: Combined data type arrays.

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 24th, 2006
0

Re: Combined data type arrays.

The good news is that you can write your class much cleaner like this:
C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  1. struct MyStorage
  2. {
  3. string MyString;
  4. double MyDecimal;
  5. int MyInt;
  6. };

Cool huh?
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Mar 24th, 2006
0

Re: Combined data type arrays.

Real cool! Thanks,

Quote ...
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.
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Podge is offline Offline
13 posts
since Mar 2006
Mar 25th, 2006
0

Re: Combined data type arrays.

I think this will work. Haven't compiled/run to prove it though.
C++ Syntax (Toggle Plain Text)
  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?
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Ancient Dragon
Next Thread in C++ Forum Timeline: Passing a matrix from main function to user defined function.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC