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. Strange that CFont::GetLogFont not const

Strange that CFont::GetLogFont not const

Scheduled Pinned Locked Moved C / C++ / MFC
comquestionannouncement
2 Posts 2 Posters 2 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.
  • M Offline
    M Offline
    Moak
    wrote on last edited by
    #1

    really strange... did I oversee something? Those WinAPI stuff confuses me sometimes... but i see no reason why it can't be const. I needed a const version so I did this:

    // Get LOGFONT structure
    BOOL CMyFonts::GetLogFont(const CFont& Font, LOGFONT* pLogFont) const
    {
    ASSERT(pLogFont);
    HFONT hFont = Font;
    return ::GetObject(hFont, sizeof(LOGFONT), pLogFont);
    }

    U 1 Reply Last reply
    0
    • M Moak

      really strange... did I oversee something? Those WinAPI stuff confuses me sometimes... but i see no reason why it can't be const. I needed a const version so I did this:

      // Get LOGFONT structure
      BOOL CMyFonts::GetLogFont(const CFont& Font, LOGFONT* pLogFont) const
      {
      ASSERT(pLogFont);
      HFONT hFont = Font;
      return ::GetObject(hFont, sizeof(LOGFONT), pLogFont);
      }

      U Offline
      U Offline
      User 5680157
      wrote on last edited by
      #2

      Just look what I do. The important line can work on a const method! I am following Scott Meyers recommendations!

      CSomeClass& CSomeClass::operator=(const CSomeClass& source)
      {
      m_name=source.m_name;

      LOGFONT lf;
      (const\_cast(static\_cast (source.m\_font)).GetLogFont(&lf)); //this is the important line
      m\_font.Detach();
      m\_font.CreateFontIndirect(&lf);
      
      //...
      
      return \*this;
      

      }

      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