Click here to Skip to main content
16,006,475 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionModeless dialog ? Pin
Stan the man7-Apr-02 6:05
Stan the man7-Apr-02 6:05 
AnswerRe: Modeless dialog ? Pin
Michael Dunn7-Apr-02 8:11
sitebuilderMichael Dunn7-Apr-02 8:11 
GeneralRemove button of app from task bar Pin
Stan the man7-Apr-02 6:00
Stan the man7-Apr-02 6:00 
GeneralRe: Remove button of app from task bar Pin
Paul M Watt7-Apr-02 6:12
mentorPaul M Watt7-Apr-02 6:12 
GeneralCListView and keyboard event Pin
Abin7-Apr-02 5:53
Abin7-Apr-02 5:53 
GeneralRuntime variable creation Pin
emrosa7-Apr-02 5:12
emrosa7-Apr-02 5:12 
GeneralRe: Runtime variable creation Pin
Paul M Watt7-Apr-02 7:00
mentorPaul M Watt7-Apr-02 7:00 
GeneralRe: Runtime variable creation Pin
Paul M Watt7-Apr-02 7:02
mentorPaul M Watt7-Apr-02 7:02 
Depending on what you are doing, I would recommend that you check out the Standard Template Library (STL). It makes dynamic memory allocation and management a snap, and there are a few tricks that you can do to make accessing the varaibles much easier.

At first glance the vector seems to be what you are looking for, because it is an array that will dynamically grow and manage its own memory. However there are problems with using an array, even if you are doing it the straight C++ way. As long as you will be using STL, I would recommend the map. A map you have a key, and a value that the key represents. You could make the key a string, and the value a double. Here is an example.

//There is a little bit of setup work here, but do not let this intimidate you.
//This goes in your header file.
#include <map>
using std::map;

// This is a function object that is declared to help sort the string names in your map.
struct ltstr
{
  bool operator()(const char* s1, const char* s2) const
  {
    return strcmp(s1, s2) < 0;
  }
};

typedef map<char*, double, ltstr> mapVars;

Here is how you will use what we just setup:
mapVars vars;
// I read in your question that creating a name for your variables is not an issue, so for 
// simplicity I am going to use a statically defined string.
// Insert a new variable.
vars.insert("variable1", double*);
// Lookup the new variable.
double value = vars["variable1"];
//Remove this variable.  when you erase a variable from a vector or a dynamic array, you will have
//gaps, this is transparently handled with a map.
vars.erase("variable1");


Now you have a set of dynamically defined variables with names. Now you need a way to dynamically allocate the array of variables. This case would either be good for a vector or a map. Both of these objects will manage their own memory, and grow automatically for your. Here is how you would use the vector:

// Once again, this goes into your header file.
// Also use the same code as above for the map.
#include <vector>
using std::vector;

typedef vector<double> vecDouble;
typedef map<char*, vector<double>, ltstr> mapVecVars;
// Here is how to use your new dynamic 2 dymensional variable vectors.
// First create the new varaible, which will actually be a vector.
mapVecVars vars;
vecDouble newVar;
// Add new doubles values into your new var.
newVar.push_back(1.0);
newVar.push_back(3.14);
newVar.push_back(98.6);
// Add the new vector of variables to your map.
vars.insert("variable1", newVar);

// You can now access the variables in "vars" like this:
cout << vars["variable1"][0] << endl;
// this would print out "1.0"
cout << vars["variable1"][2] << endl;
// this would print out "98.6"

// You can access the data that way because it is like calling this:
// vector<double> vec = vars["varaible1"];
// cout << vec[0] << endl;


For efficiency, you may want to hold pointers to doubles, and vector<doubles> to prevent all of the data from being copied each time it is accessed, however, the concepts are pretty much the same.

There is a lot of information here, on something that has a million ways to implement. This way I believe will get you up and running the quickest.

The arrays will work, but they will get messy when you have more than a few dynamic variables, and you start to erase varaibles in the middle of the array.

Good Luck


Checkout my Guide to Win32 Paint for Intermediates
GeneralConsole Application with a dialog box Pin
caoimhinfisher@mac.com7-Apr-02 4:39
caoimhinfisher@mac.com7-Apr-02 4:39 
GeneralDesktop Special Folder Detection Pin
Darren Schroeder7-Apr-02 4:17
Darren Schroeder7-Apr-02 4:17 
GeneralRe: Desktop Special Folder Detection Pin
Shotgun7-Apr-02 4:49
Shotgun7-Apr-02 4:49 
Generallast dumb question Pin
Shotgun7-Apr-02 4:00
Shotgun7-Apr-02 4:00 
GeneralRe: last dumb question Pin
Renjith Ramachandran7-Apr-02 5:27
Renjith Ramachandran7-Apr-02 5:27 
GeneralUsing Control to Display Unicode String Pin
7-Apr-02 3:56
suss7-Apr-02 3:56 
QuestionHow to change the size of a row in splitter wnd? Pin
Feng Qin7-Apr-02 3:47
Feng Qin7-Apr-02 3:47 
GeneralDrawing lines Pin
Jonny827-Apr-02 2:11
Jonny827-Apr-02 2:11 
GeneralRe: Drawing lines Pin
Rickard Andersson207-Apr-02 3:15
Rickard Andersson207-Apr-02 3:15 
GeneralDocking windows without MFC Pin
6-Apr-02 23:55
suss6-Apr-02 23:55 
GeneralSearch *.exe and *.com files Pin
Phlegm6-Apr-02 23:46
Phlegm6-Apr-02 23:46 
GeneralRe: Search *.exe and *.com files Pin
Mazdak7-Apr-02 1:10
Mazdak7-Apr-02 1:10 
GeneralRe: Search *.exe and *.com files Pin
Dominik Reichl7-Apr-02 1:07
Dominik Reichl7-Apr-02 1:07 
GeneralRe: Search *.exe and *.com files Pin
Renjith Ramachandran7-Apr-02 5:22
Renjith Ramachandran7-Apr-02 5:22 
GeneralRead line per line using CFile object Pin
Atlence6-Apr-02 23:42
Atlence6-Apr-02 23:42 
GeneralRe: Read line per line using CFile object Pin
Christian Graus6-Apr-02 23:55
protectorChristian Graus6-Apr-02 23:55 
GeneralRe: Read line per line using CFile object Pin
Nish Nishant7-Apr-02 0:08
sitebuilderNish Nishant7-Apr-02 0:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.