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. Questions about defining a funciton

Questions about defining a funciton

Scheduled Pinned Locked Moved C / C++ / MFC
question
9 Posts 5 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.
  • N Offline
    N Offline
    nachilau
    wrote on last edited by
    #1

    Hello All, Can anyone tell me that what is the different when declare function like const char* f(); and char* const f(); I would like to know what does this two function signature means. Thanks! :)Nachi

    T D M 3 Replies Last reply
    0
    • N nachilau

      Hello All, Can anyone tell me that what is the different when declare function like const char* f(); and char* const f(); I would like to know what does this two function signature means. Thanks! :)Nachi

      T Offline
      T Offline
      toxcct
      wrote on last edited by
      #2

      easy, the second don't exist ! lol in fact, you might want to say

      const char* f();
      char* f const();

      that's simple. the first one returns a const char* when the second one returns a simple char*. What does the second const mean, you'll ask me !? In fact, that is used in object programming, when you read values in an object, where you don't alterate that object... In brief, it means that you don't modify the members of your objet... you don't need this in pure C so... see you ;)

      TOXCCT >>> GEII power

      N 1 Reply Last reply
      0
      • N nachilau

        Hello All, Can anyone tell me that what is the different when declare function like const char* f(); and char* const f(); I would like to know what does this two function signature means. Thanks! :)Nachi

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        Did this not help?


        "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

        N 1 Reply Last reply
        0
        • T toxcct

          easy, the second don't exist ! lol in fact, you might want to say

          const char* f();
          char* f const();

          that's simple. the first one returns a const char* when the second one returns a simple char*. What does the second const mean, you'll ask me !? In fact, that is used in object programming, when you read values in an object, where you don't alterate that object... In brief, it means that you don't modify the members of your objet... you don't need this in pure C so... see you ;)

          TOXCCT >>> GEII power

          N Offline
          N Offline
          nachilau
          wrote on last edited by
          #4

          Ummm, You said the second one char* const f(); does not exist, but if I write this in Visual C++, the compiler does not complain. Also, for the first one, I can actualy return something which is not a const, eg, const int f() { int i = 10; return i; } and it also works in Visual C++ too. And would you mind to tell me why's that? Thank you very much! ;) Nachi

          A 1 Reply Last reply
          0
          • D David Crow

            Did this not help?


            "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

            N Offline
            N Offline
            nachilau
            wrote on last edited by
            #5

            Sorry, it doesn't really help, I know how to use a constant data in a program, but I have some confussion when using in the definition of functions Thanks! Nachi

            D 1 Reply Last reply
            0
            • N nachilau

              Ummm, You said the second one char* const f(); does not exist, but if I write this in Visual C++, the compiler does not complain. Also, for the first one, I can actualy return something which is not a const, eg, const int f() { int i = 10; return i; } and it also works in Visual C++ too. And would you mind to tell me why's that? Thank you very much! ;) Nachi

              A Offline
              A Offline
              Alexander M
              wrote on last edited by
              #6

              Stop playing silly games in here, returning a const value doesn't make any sense! Don't try it, just do it! ;-)

              N 1 Reply Last reply
              0
              • N nachilau

                Sorry, it doesn't really help, I know how to use a constant data in a program, but I have some confussion when using in the definition of functions Thanks! Nachi

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                So doesn't it make more sense to continue that thread rather than creating a new one of the same topic? In any case, MSDN has this to say about const: When modifying a data declaration, the const keyword specifies that the object or variable is not modifiable. When following a member function's parameter list, the const keyword specifies that the function doesn't modify the object for which it is invoked. With C++, const is often used in place of the #define preprocessor directive. Values defined with const are subject to type checking, and can be used in place of constant expressions. When const is used with pointers, it specifies that the pointer cannot be modified after initialization; the pointer is protected from modification thereafter. So, const char *cpch means that the object pointed to by the pointer is const. And, char *const pchc means that the value of the pointer — the actual address stored in the pointer — is const.


                "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

                1 Reply Last reply
                0
                • A Alexander M

                  Stop playing silly games in here, returning a const value doesn't make any sense! Don't try it, just do it! ;-)

                  N Offline
                  N Offline
                  nachilau
                  wrote on last edited by
                  #8

                  Hello, You are right, I know that it does not make any sense, but I still don't know what is this, int const f(); even thought I never use it in my programming experience. But in some C++ test, people ask something about this, and what I know and what I have used is only something like this, void f(const int); or void f(int) const; and these both make sense to me, but not int const f(); therefore, the's why I would like to know whether this signature make sense to any of you. But anyway, thank you for all your suggestio! Nachi

                  1 Reply Last reply
                  0
                  • N nachilau

                    Hello All, Can anyone tell me that what is the different when declare function like const char* f(); and char* const f(); I would like to know what does this two function signature means. Thanks! :)Nachi

                    M Offline
                    M Offline
                    Michael Dunn
                    wrote on last edited by
                    #9

                    const char* means "pointer to a constant char" - you cannot change the value of the char that is pointed to, but you can change the pointer to point at some other char. char* const means "constant pointer to a char" - you can change the value of the char pointed to, but you cannot change the pointer to point a some other char. And const char* const means a combination of the two. :) Example code:

                    char c = 'c';
                    char d = 'd';
                    const char* p1 = &c;
                    char* const p2 = &c;

                    *p1 = 'x'; // illegal
                    p1 = &d; // legal

                    *p2 = 'z'; // legal
                    p2 = &d; // illegal

                    --Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ ---- Laugh it up, fuzzball.

                    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