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. What is the best way of handling a code like

What is the best way of handling a code like

Scheduled Pinned Locked Moved C / C++ / MFC
questiondatabaseregexcode-review
2 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.
  • C Offline
    C Offline
    ComplexLifeForm
    wrote on last edited by
    #1

    Hi, I would like to know if there is a better way of writing a particular pattern of the code as mentioned below. I have been encountering this type quite frequently and do not like the way it is implemented. The code piece is as follows class Address { public: Address():m_address("") {} Address(const CString& addr) : m_address(addr) {} CString GetAddress()------------------ (1) { return m_address; } CString GetAddress() const ------------------ (1a) { return m_address; } const CString& GetAddress() const -----(2) { return m_address; } private: CString m_address; }; Now somewhere in main method Address addr("Living somewhere on Earth"); further down a call is made into a third party library which only takes char* and has a method like void FormatAddress(char* address); -------(3) Now if I make a call to this method, I must do this FormatAddress(const_cast<char*>(addr.GetAddress().GetString()); -------------(4) So my query is whether the whole piece of code is correct, sure it does compile and gives the result as expected but I am looking to improve my programming skills and want to write a better and cleaner code. Out of the methods 1 and 1a which is more correct? Does it makes sense to make the m_address variable as mutable. Thanks and Regards :) :)

    K 1 Reply Last reply
    0
    • C ComplexLifeForm

      Hi, I would like to know if there is a better way of writing a particular pattern of the code as mentioned below. I have been encountering this type quite frequently and do not like the way it is implemented. The code piece is as follows class Address { public: Address():m_address("") {} Address(const CString& addr) : m_address(addr) {} CString GetAddress()------------------ (1) { return m_address; } CString GetAddress() const ------------------ (1a) { return m_address; } const CString& GetAddress() const -----(2) { return m_address; } private: CString m_address; }; Now somewhere in main method Address addr("Living somewhere on Earth"); further down a call is made into a third party library which only takes char* and has a method like void FormatAddress(char* address); -------(3) Now if I make a call to this method, I must do this FormatAddress(const_cast<char*>(addr.GetAddress().GetString()); -------------(4) So my query is whether the whole piece of code is correct, sure it does compile and gives the result as expected but I am looking to improve my programming skills and want to write a better and cleaner code. Out of the methods 1 and 1a which is more correct? Does it makes sense to make the m_address variable as mutable. Thanks and Regards :) :)

      K Offline
      K Offline
      KarstenK
      wrote on last edited by
      #2

      LPCTSTR GetAddress() const { return m_address; } as the only access function when void FormatAddress(...) is NOT changing the input paramters X|

      Greetings from Germany

      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