| | |
Combined data type arrays.
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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
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
•
•
Join Date: Jul 2005
Posts: 1,671
Reputation:
Solved Threads: 261
The good news is that you can write your class much cleaner like this:
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.
And because in C++ structs are the same as classes with member variables having public access by default you even simplify to this:
Cool huh?
C++ Syntax (Toggle Plain Text)
class MyStorage { public: MyStorage() { } string MyString; double MyDecimal; int MyInt; };
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)
class MyStorage { public: string MyString; double MyDecimal; int MyInt; };
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)
struct MyStorage { string MyString; double MyDecimal; int MyInt; };
Cool huh?
Real cool! Thanks,
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.
•
•
•
•
if you aren't going to initialize variables to a default value, use dynamic memory for variables, etc.,
typedef vector< MyStorage^ > MY_OBJ;
I can’t seem to get the concept.
•
•
Join Date: Jul 2005
Posts: 1,671
Reputation:
Solved Threads: 261
I think this will work. Haven't compiled/run to prove it though.
>>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?
C++ Syntax (Toggle Plain Text)
#include <iostream> #incldue <string> #include <vector> using namespace std; struct MyStorage { //default constructor MyStorage(): MyString(""), MyDecimal(0.0), MyInt(0){} //three parameter constructor MyStorage(string s, double d, int i) : MyString(s), MyDecimal(d), MyInt(i) {} //copy constructor MyStorage(const MyStorage & m) : MyString(m.MyString), MyDecimal(m.MyDecimal), MyInt(m.MyInt) {} //variables string MyString; double MyDecimal; int MyInt; //overloaded << operator to simplify printing contents of MyStorage objects friend ostream & operator<<(ostream & os, const MyStorage & m) { os << m.MyString << ' ' << m.MyDecimal << ' ' << m.MyInt; return os; } }; int main() { vector<MyStorage> myVector; string str = "trial"; int i; const int MAX = 3; for(i = 0; i < MAX; ++i) myVector.push_back(MyStorage temp(str, i * 1.23, i )); vector<MyStorage>::iterator start = myVector.begin(); vector<MyStorage>::iterator stop = myVector.end(); for( ; start != stop; ++start) cout << *start << endl; }
>>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?
![]() |
Similar Threads
- Working with SQL server's Image data type (ASP.NET)
- Help With Data-type Declaration Symbols (Pascal and Delphi)
- Working with SQL's Text data type (ASP.NET)
- Text data type troubles in T-SQL (Database Design)
Other Threads in the C++ Forum
- Previous Thread: Ancient Dragon
- Next Thread: Passing a matrix from main function to user defined function.
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator 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 int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector visualstudio win32 windows winsock word wordfrequency wxwidgets






