|
Hello,
I am not sure how this works I am just beginning to learn this.
A and B are objects of the same class named TestClass
in my main i have:
...
if (A != B)
...
.cpp file has the definitions:
this class has one data member:
char* m_Str
//comparison operator !=
bool MGString::operator!=(const char* Src) const
{
bool value;
value = Compare(Src,m_Sensitive);
return value;
}
//compare function
int MGString::Compare(const char* Src, bool m_Sensitive) const
{
int result;
if (! m_Sensitive)
{
result = strcmp(m_Str,Src);
}
else
{
result = strncmp(m_Str,Src,strlen(lino));
}
if (result !=0)
return false;
else
return true;
}
//conversion operator
MGString::operator const char*()
{
return m_Str;
}
this will not compile because strlen can not accept an argument (m_Str) of type TestClass it needs a char*.
I am guessing that the way I have it set up now my conversion operator returns type char* for the data memeber m_Str for object B. However the m_Str for object A is still of type TestClass so in main when the conversion operator is called for B i think it returns a convertred value for m_Str from TestClass to char*. How do I fix this?
thanks,
Lino
|
|
|
|
|
bool operator!=(const TestClass& s1, const TestClass& s2)
{
return Compare(s1.m_Str, s2.m_Str);
}
"Opinions are neither right nor wrong. I cannot change your opinion of me. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
can u elaborate? why is this not a class member?
and i do want to use a conversion operator...are u telling that it would be incorrect of me to use a conversion operator?
can u show me how to use the conversion operator?
thanks,
Lino
|
|
|
|
|
lino_i wrote:
can u elaborate? why is this not a class member?
That was just an example. You could also make it a member, like:
class TestClass
{
operator LPCTSTR() const
{
return m_Str;
}
bool operator!=(TestClass& s1) const
{
return Compare(m_Str, s1.m_Str);
}
};
"Opinions are neither right nor wrong. I cannot change your opinion of me. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
thanks for your patience...
again i am a beginner at all of this but...
looking at this again why do i even need a conversion operator if m_Str is declared a char* private data member when i build my class.
why doesn't this line of code:
if (A != B) call:
bool operator!=(TestClass& sl) const
{
return Compare(m_Str, sl.m_Str);
}
what's the point of the conversion operator?
and in your scenario above what is actually getting converted (the data member of which object?)
thanks,
Lino
|
|
|
|
|
lino_i wrote:
why doesn't this line of code:
if (A != B) call:
bool operator!=(TestClass& sl) const
{
return Compare(m_Str, sl.m_Str);
}
This works fine for me:
class TestClass
{
char *m_Str;
bool Compare(const char *str1, const char *str2) const
{
return (strcmp(str1, str2) == 0);
}
public:
TestClass(){}
~TestClass(){}
operator LPCTSTR() const
{
return m_Str;
}
bool operator!=(TestClass& s1) const
{
return Compare(m_Str, s1.m_Str);
}
}; lino_i wrote:
what's the point of the conversion operator?
If you needed to call a function or method that knew nothing about a TestClass object, but it did understand what a const char* was, it would be used in that context.
"Opinions are neither right nor wrong. I cannot change your opinion of me. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
you have a conversion operator as well...why is it necessary? m_Str is already a pointer?
|
|
|
|
|
lino_i wrote:
why is it necessary?
I didn't say that it was. I was just showing you how to implement one. If there is never a need for users of your class to access m_Str , it can be removed. However, if you did want users of your class to be able to access m_Str , it would be needed. In your case, it would not be considered a "conversion" operator, but simply an "access" operator.
"Opinions are neither right nor wrong. I cannot change your opinion of me. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
There is lots of stuff around on skinning dialogs and dialog controls, but has anyone done anything with skinned CPropertySheet and skinned CPropertyPage ???
|
|
|
|
|
Hello,
I have a function that calls a compare function which does a simple strcmp() comparison. Then in this function I return to the calling function the value of result.
this is my compare function:
bool m_Sensitive = false;
int result;
char lino[] = "value";
if (! m_Sensitive)
{
result = strcmp(lino,Src);
}
else
{
result = strncmp(lino,Src,strlen(lino));
}
return result;
Back in the calling function which returns a bool I need to analyze the return value.
I have tried
if (return !=0)
return false;
else
return true;
this is not working any thoughts?
thanks,
Lino
|
|
|
|
|
return m_Sensitive ? strncmp(lino, Src, strlen(lino) ) == 0 : strcmp(lino,Src)== 0 ;
returns TRUE if the strings match else FALSE
|
|
|
|
|
You've not indicated what is not working. The code snippet you've shown is syntactically correct.
"Opinions are neither right nor wrong. I cannot change your opinion of me. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
In MS Visual C++, how do I gray out a dialog's radio button *IN CODE* as opposed to using the resource editor?
|
|
|
|
|
Call EnableWindow(FALSE) .
"Opinions are neither right nor wrong. I cannot change your opinion of me. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
Thanks David! I used the following code...
GetDlgItem(IDC_RADIO1)->EnableWindow(FALSE);
|
|
|
|
|
Like this ...
<br />
void CMyDlg::EnableField(UINT nID, BOOL bEnable)<br />
{<br />
CWnd *pWnd = GetDlgItem( nID );<br />
ASSERT( pWnd );<br />
pWnd->EnableWindow( bEnable) ;<br />
}<br />
<br />
EnableField( IDC_RADIOxxx, FALSE);
Uncomment the ShowWindow statement to re-inforce the fact that a dialog window is disabled by hiding it.
|
|
|
|
|
Thanks mtzlplyk! Useful code there!
|
|
|
|
|
I am having trouble getting my Picking code to work in collaboration with glUnProject. The picking will work with the use of glUnProject but I need it to work for me. anybody else run into that?
|
|
|
|
|
Hi.
I’m a VC++ developer that is going to distribute exactly 10 licenses of an ActiveX control I’ve done. I would like to make the ActiveX itself freely available on for e.g. a public ftp-server. So, about 100 users have may installed my ActiveX but because my customer just bought license for 10, only 10 users should bee able to use it at the same time.
I’ve figured of this a lot and have found out that all controls may have some connection to a server that “allow” them to run. If that’s a good thought, how should I design that “server-program”?
Would be very pleased of tips of where to find different ways to handle license and login techniques!
In which way is preferred to handle my problem? All thoughts are helpful!
_____________________________
...and justice for all
APe
|
|
|
|
|
I have written an MFC App that will display a hard coded menu item CString to the DC (the screen) in 3 diffetrnt RGB values using 'Set' and 'Get' functions to randomize the color. I have a random number generator that passes the value to a switch statement with RGB colors.
I now want to change the code to rendomize the color of EACH INDIVIDUAL CHARACTER in the string, as opposed to having the whole string the same color.
If some one could send a code fragment to access each character in s CString.
I'm using DrawText to display.
- thanks for your help
|
|
|
|
|
SkunkedWorks wrote:
If some one could send a code fragment to access each character in s CString.
No special code required. Just use the [] operator or the GetAt() method.
"Opinions are neither right nor wrong. I cannot change your opinion of me. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
Thanks.
So... say I have this:
CString str("abc");
What would the syntax look like if i used GetAt()?
|
|
|
|
|
TCHAR c = str.GetAt(0 );
"Opinions are neither right nor wrong. I cannot change your opinion of me. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
Thanks David - that's EXACTLY what I was looking for. When I searched for it in MSDN it kept giving me a reference to a CHString (whatever that is..)
|
|
|
|
|
It's similar to CString but without MFC's overhead. The WMI classes seem to make extensive use of it.
"Opinions are neither right nor wrong. I cannot change your opinion of me. I can, however, change what influences your opinion." - David Crow
|
|
|
|