Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CtlColor question

CtlColor question

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++help
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bulg
    wrote on last edited by
    #1

    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?

    B S 2 Replies Last reply
    0
    • B bulg

      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?

      B Offline
      B Offline
      bulg
      wrote on last edited by
      #2

      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.

      S 1 Reply Last reply
      0
      • B bulg

        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?

        S Offline
        S Offline
        Stuart Dootson
        wrote on last edited by
        #3

        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

        B 1 Reply Last reply
        0
        • B bulg

          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.

          S Offline
          S Offline
          Stuart Dootson
          wrote on last edited by
          #4

          CTopDlg::OnCtlColor should be a valid reference within CDerivedDlg even if it is declared virtual. It tells the compiler explicitly to call OnCtlColor as if this had type CTopDlg. It certainly works in VS2008 and g++4.0.1

          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

          1 Reply Last reply
          0
          • S Stuart Dootson

            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

            B Offline
            B Offline
            bulg
            wrote on last edited by
            #5

            (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)

            S 1 Reply Last reply
            0
            • B bulg

              (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)

              S Offline
              S Offline
              Stuart Dootson
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups