CString reference
-
How can I declare a CString reference into a generic class ? In fact, I want to declare a CString reference that could store errors, but that CString variable to be part of external class (let's say CMyWinApp class) ... it's ilogical request ? I try to do :
// CMyClass.h
CString& m_sErrorExt;but I get follow error :
'm_sErrorExt' : must be initialized in constructor base/member initializer list
-
How can I declare a CString reference into a generic class ? In fact, I want to declare a CString reference that could store errors, but that CString variable to be part of external class (let's say CMyWinApp class) ... it's ilogical request ? I try to do :
// CMyClass.h
CString& m_sErrorExt;but I get follow error :
'm_sErrorExt' : must be initialized in constructor base/member initializer list
Reference and const member variables must be given a value when initialized or in the constructor. Google for examples of how to resolve error C2758.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
-
How can I declare a CString reference into a generic class ? In fact, I want to declare a CString reference that could store errors, but that CString variable to be part of external class (let's say CMyWinApp class) ... it's ilogical request ? I try to do :
// CMyClass.h
CString& m_sErrorExt;but I get follow error :
'm_sErrorExt' : must be initialized in constructor base/member initializer list
You need to initialize it in the constructor.
class CMyClass
{
public:
CString m_DefaultErrorExt;
CString & m_sErrorExt;public:
CMyClass(void) : sErrorExt(DefaultErrorExt) {}
CMyClass(CString & ExternError) : sErrorExt(ExternError) {}
}; -
You need to initialize it in the constructor.
class CMyClass
{
public:
CString m_DefaultErrorExt;
CString & m_sErrorExt;public:
CMyClass(void) : sErrorExt(DefaultErrorExt) {}
CMyClass(CString & ExternError) : sErrorExt(ExternError) {}
};With this said, this can be a dangerous practice. If the object your object is linked to, goes out of scope, your code will crash when it tries to access the reference. I don't use this technique often. In my code, my most common usage is when I'm working with legacy code that has really bad variable names. I'll sometimes create a new variable with a meaningful name, and convert the old one to a reference, so that it's still available to code I don't intend to touch.
-
With this said, this can be a dangerous practice. If the object your object is linked to, goes out of scope, your code will crash when it tries to access the reference. I don't use this technique often. In my code, my most common usage is when I'm working with legacy code that has really bad variable names. I'll sometimes create a new variable with a meaningful name, and convert the old one to a reference, so that it's still available to code I don't intend to touch.
In my class, I do something that could throw into exceptions, so, I put an string reference in method paramter, like this :
BOOL CMyClass::Execute(LPCTSTR lpszParam1, CString& sError);
so, I give up the first request ... I don't know if is good ideea ... thank you all anyway.
-
How can I declare a CString reference into a generic class ? In fact, I want to declare a CString reference that could store errors, but that CString variable to be part of external class (let's say CMyWinApp class) ... it's ilogical request ? I try to do :
// CMyClass.h
CString& m_sErrorExt;but I get follow error :
'm_sErrorExt' : must be initialized in constructor base/member initializer list
The way you describe it, that string sounds like an object property - the last error message - so it's better and simpler to let it be a member of your class and add an accessor function for it...
class MyClass
{
public:
bool execute()
{
try
{
// ...
throw exception("Testing testing");
return true;
}
catch(exception& e)
{
m_lastError = e.what();
return false;
}
}
CString& lastError()
{
return m_lastError;
}
private:
CString m_lastError;
};void tryIt()
{
MyClass c;
if( c.execute() )
cout << "All well" << endl;
else
cout << "MyClass.execute failed: " << c.lastError().GetString() << endl;
}This is safer because you know the CString goes out of scope at the same time your class does, and you still have a CString reference available to use from the rest of your code exactly as before.
-
In my class, I do something that could throw into exceptions, so, I put an string reference in method paramter, like this :
BOOL CMyClass::Execute(LPCTSTR lpszParam1, CString& sError);
so, I give up the first request ... I don't know if is good ideea ... thank you all anyway.
I don't understand what you're saying there.
-
In my class, I do something that could throw into exceptions, so, I put an string reference in method paramter, like this :
BOOL CMyClass::Execute(LPCTSTR lpszParam1, CString& sError);
so, I give up the first request ... I don't know if is good ideea ... thank you all anyway.
-
The way you describe it, that string sounds like an object property - the last error message - so it's better and simpler to let it be a member of your class and add an accessor function for it...
class MyClass
{
public:
bool execute()
{
try
{
// ...
throw exception("Testing testing");
return true;
}
catch(exception& e)
{
m_lastError = e.what();
return false;
}
}
CString& lastError()
{
return m_lastError;
}
private:
CString m_lastError;
};void tryIt()
{
MyClass c;
if( c.execute() )
cout << "All well" << endl;
else
cout << "MyClass.execute failed: " << c.lastError().GetString() << endl;
}This is safer because you know the CString goes out of scope at the same time your class does, and you still have a CString reference available to use from the rest of your code exactly as before.
Here is what I'm going to do :
CMyClass
{
BOOL Execute(LPCTSTR lpszSQL,CString& sError);
}BOOL CMyClass::Execute(LPCTSTR lpszSQL,CString& sError)
{
try
{
throw ....
}
catch(CException* e)
{
e->GetErrorMessage(sError.GetBufer(255),255);
sError->ReleaseBuffer();
e->Delete();
}
}somewhere, along 'Execute' method, might have an error, and want to have method feedback and error description in one code line :
CMyClass obj;
CString sError;
if(! obj.Execute(_T("SELECT * FROM mytable"),sError))
{
MessageBox(sError);
return;
}which in fact, it's the same thing like you describe above ... First time, I was thinking that I could setup an external CString reference to have all posible error in CMyClass, but now I see that is not the best idea ... But, one for another, I learn something here because of you guys, and for that I will kindly thank you.