CtlColor question
-
Given this...
class CTopDlg : public CDialog {
...then this works...
HBRUSH CTopDlg::OnCtlColor(CDC*,CWnd*,UINT){
HBRUSH ret = CDialog::OnCtlColor(...)
...
return ret
}but if I try this...
class CDerivedDlg : public CTopDlg {
...then this does _not_ work...
HBRUSH CDerivedDlg::OnCtlColor(CDC*,CWnd*,UINT) {
HBRUSH ret = CTopDlg::OnCtlColor(...) // error: access non-static protected member function
...
return ret
}OnCtlColor is declared virtual in my CTopDlg class, and is also in a protected: section. It is _not_ virtual and _is_ protected in CDialog. The error is based on the rules of C++, but how did CTopDlg compile?
-
Given this...
class CTopDlg : public CDialog {
...then this works...
HBRUSH CTopDlg::OnCtlColor(CDC*,CWnd*,UINT){
HBRUSH ret = CDialog::OnCtlColor(...)
...
return ret
}but if I try this...
class CDerivedDlg : public CTopDlg {
...then this does _not_ work...
HBRUSH CDerivedDlg::OnCtlColor(CDC*,CWnd*,UINT) {
HBRUSH ret = CTopDlg::OnCtlColor(...) // error: access non-static protected member function
...
return ret
}OnCtlColor is declared virtual in my CTopDlg class, and is also in a protected: section. It is _not_ virtual and _is_ protected in CDialog. The error is based on the rules of C++, but how did CTopDlg compile?
brain fart.. here's why in case you wasted your time with my first post, or you looked this up: make OnCtlColor not virtual in CTopDlg. Then it isn't overwritten by CDerivedDlg::OnCtlColor, and CTopDlg::OnCtlColor(...) is a valid reference inside the CDerivedDlg version.
-
Given this...
class CTopDlg : public CDialog {
...then this works...
HBRUSH CTopDlg::OnCtlColor(CDC*,CWnd*,UINT){
HBRUSH ret = CDialog::OnCtlColor(...)
...
return ret
}but if I try this...
class CDerivedDlg : public CTopDlg {
...then this does _not_ work...
HBRUSH CDerivedDlg::OnCtlColor(CDC*,CWnd*,UINT) {
HBRUSH ret = CTopDlg::OnCtlColor(...) // error: access non-static protected member function
...
return ret
}OnCtlColor is declared virtual in my CTopDlg class, and is also in a protected: section. It is _not_ virtual and _is_ protected in CDialog. The error is based on the rules of C++, but how did CTopDlg compile?
The message indicates the compiler can't get a CTopDlg object pointer from the CDerivedDlg object pointer you get using
this
(BTW - what error number are you getting?). Your scenario, as presented, (OnCtlColor declared virtual and protected in CTopDlg, OnCtlColor declared virtual (or non-virtual) and protected in CDerivedDlg) compiles under VS2008. So...what compiler are you using (VC6? I'm thinking it might get confused easier than VS2008), are you absolutely sure you've derived CDerivedDlg publicly from CTopDlg?Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
brain fart.. here's why in case you wasted your time with my first post, or you looked this up: make OnCtlColor not virtual in CTopDlg. Then it isn't overwritten by CDerivedDlg::OnCtlColor, and CTopDlg::OnCtlColor(...) is a valid reference inside the CDerivedDlg version.
CTopDlg::OnCtlColor
should be a valid reference within CDerivedDlg even if it is declared virtual. It tells the compiler explicitly to call OnCtlColor as ifthis
had type CTopDlg. It certainly works in VS2008 and g++4.0.1Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
The message indicates the compiler can't get a CTopDlg object pointer from the CDerivedDlg object pointer you get using
this
(BTW - what error number are you getting?). Your scenario, as presented, (OnCtlColor declared virtual and protected in CTopDlg, OnCtlColor declared virtual (or non-virtual) and protected in CDerivedDlg) compiles under VS2008. So...what compiler are you using (VC6? I'm thinking it might get confused easier than VS2008), are you absolutely sure you've derived CDerivedDlg publicly from CTopDlg?Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
(busted!) Yes, I'm using VC6, since for some reason that's what our partners chose to code in originally. I thought it was a blessing in disguise, as if CDerivedDlg::OnCtlColor ever got executed, with a valid call to this->OnCtlColor, the program would almost certainly hang, right? (I can't try it on this machine)
-
(busted!) Yes, I'm using VC6, since for some reason that's what our partners chose to code in originally. I thought it was a blessing in disguise, as if CDerivedDlg::OnCtlColor ever got executed, with a valid call to this->OnCtlColor, the program would almost certainly hang, right? (I can't try it on this machine)
bulg wrote:
I thought it was a blessing in disguise, as if CDerivedDlg::OnCtlColor ever got executed, with a valid call to this->OnCtlColor, the program would almost certainly hang, right?
No, it'll do the right thing - when you use a class namespace specifier like that (i.e. something like
CTopDlg::OnCtlColor
), it determines what method to call statically at compile-time (i.e. the compiler works out what to call) rather than dynamically at run-time (i.e. looking up what to call in the v-table).Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p