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

Recommended Answers

All 4 Replies

>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.

The good news is that you can write your class much cleaner like this:

class MyStorage
{
public: 
  MyStorage()
  {
  }
  string MyString;
  double MyDecimal;
  int MyInt;
};

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.

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:

struct MyStorage
{
  string MyString;
  double MyDecimal;
  int MyInt;
};

Cool huh?

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.

I think this will work. Haven't compiled/run to prove it though.

#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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.