Passing variable NAME to a function
Language: Visual C++, MFC
Environment: Visual Studio 2005
So I posted a similar question, but I've come to realize that I was asking the wrong question. I'm trying to use a loop to call a function on several different variables, but somewhere along the way, the program is crashing.
Simplified code is below, but I think it's actually easier to just explain it. I have a function that takes in a CString as a parameter. I have several variables I wish to feed to this function, so I put their names into an array, and I'm trying to pass them to the function that way.
// THE CODE BELOW IS WHAT I HAVE, BUT IT DOES NOT WORK //
Header File:
CString m_strTop;
CString m_strLeft;
CString m_strRight;
CS开发者_开发问答tring m_strBottom;
CString *var[4];
Source File: Constructor()
CString *var[4] = {
&m_strTop
, &m_strLeft
, &m_strRight
, &m_strBottom
};
Source File: theFunction()
void myClass::DoDataExchange(CDataExchange* pDX)
{
CSAPrefsSubDlg::DoDataExchange(pDX);
for(int i = 2001, j = 0; i <= 2004; i++, j++)
{
// THE LINE BELOW IS WHERE THINGS GO WONKY, SPECIFICALLY AT &var[j]
DDX_Text(pDX, i, *var[j]); // 'i' is the ID of the textbox
}
}
-- What DDX_Text expects --
void AFXAPI DDX_Text(
CDataExchange* pDX,
int nIDC,
CString& value
);
So like I said, I just need to feed the function the actual name of the variable. At least I think. What it's actually doing is establishing a connection between a text box in a dialog and the variable where the text box's input will be stored. I'm dereferencing correctly and everything, but I don't think this is the right approach.
Thank you for any help. And to people who answered my previous question, my apologies for misrepresenting the issue.
var
is an array of pointers to CString
.
var[j]
is a pointer to CString
.
&var[j]
is a pointer to pointer to CString
.
Now you need to pass the CString
object. So you need:
DDX_Text(pDX, i, *var[j]); // dereference a pointer to CString.
Consider using std::vector
instead of the C-array. It would be:
std::vector<CString> var(4);
...
DDX_Text(pDX, i, var[j]); // pass a CString object
I've noted that you're declaring variable var
once again in the constructor:
CString *var[4] = { // this declares new temporary variable,
// it doesn't initialize one from the header file
&m_strTop
, &m_strLeft
, &m_strRight
, &m_strBottom
};
Shouldn't it be? :
var[0] = &m_strTop;
var[1] = &m_strLeft;
var[2] = &m_strRight;
var[3] = &m_strBottom;
I suppose you need the following:
// header file
class myClass
{
std::vector<CString> var_;
...
};
// source file
myClass::myClass() : var_(4)
{
...
}
void myClass::theFunction(CDataExchange* pDX)
{
CSAPrefsSubDlg::DoDataExchange(pDX);
for(int i = 2001, j = 0; i <= 2004; i++, j++)
{
DDX_Text(pDX, i, var_[j]); // 'i' is the ID of the textbox
}
}
You're not passing the right thing into DDX_Text. It's third parameter is a reference to a CString. You're passing in the address of a pointer. So you should probably do something like
DDX_Test(pDX, i, *var[j]);
精彩评论