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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Basic question in C/ C++

Basic question in C/ C++

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++tutorial
4 Posts 4 Posters 1 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.
  • J Offline
    J Offline
    Javagal Srinath
    wrote on last edited by
    #1

    Hi I have a theoritical doubt in c/c++, that at the end of some functions I am seeing a const keyword. I dont know what is the purpose behind this. for example int function(int funcpar1,int funcpar2)const; I want to know purpose of the const keyword at the end Thanks

    V K S 3 Replies Last reply
    0
    • J Javagal Srinath

      Hi I have a theoritical doubt in c/c++, that at the end of some functions I am seeing a const keyword. I dont know what is the purpose behind this. for example int function(int funcpar1,int funcpar2)const; I want to know purpose of the const keyword at the end Thanks

      V Offline
      V Offline
      Viorel
      wrote on last edited by
      #2

      This means that the function does not modify data members of the class and does not call other functions which are not marked as const. Functions that just read and return information must be made const. Otherwise the following sample will not work:

      const CMyString s = "Test";
      int length = s.GetLength(); // Does not work if GetLength function is not marked as const
      

      This is because the s variable was declared as constant, se we cannot modify it. -- modified at 6:33 Friday 30th June, 2006

      1 Reply Last reply
      0
      • J Javagal Srinath

        Hi I have a theoritical doubt in c/c++, that at the end of some functions I am seeing a const keyword. I dont know what is the purpose behind this. for example int function(int funcpar1,int funcpar2)const; I want to know purpose of the const keyword at the end Thanks

        K Offline
        K Offline
        Kevin McFarlane
        wrote on last edited by
        #3

        Have a read of the "Methods" section here under C++ syntax: http://en.wikipedia.org/wiki/Const_correctness[^] Also see the links at the bottom. Kevin

        1 Reply Last reply
        0
        • J Javagal Srinath

          Hi I have a theoritical doubt in c/c++, that at the end of some functions I am seeing a const keyword. I dont know what is the purpose behind this. for example int function(int funcpar1,int funcpar2)const; I want to know purpose of the const keyword at the end Thanks

          S Offline
          S Offline
          Sarath C
          wrote on last edited by
          #4

          In those const functions, we could not change an object's state (be more specific member variables value) examples are some "Get" interfaces, those function's (most probably) are not supposed to change it's object state, it is for returning something that belong to the object. Putting const at end of function meaningfully is a good practice. SaRath.
          "It is your attitude, not your aptitude, that determines your altitude - Zig Ziglar." My Blog | Understanding State Pattern in C++

          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