1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++ How to declare/initialize a constant in a class?

  1. Jul 16, 2007 #1
    1. The problem statement, all variables and given/known data
    I have my computer science exam today (in 2 hours). I want to know how to declare and initialize a const member in a class.

    3. The attempt at a solution

    I tried this:

    _________________________________________________________________
    class stu
    {
    const int a=10; // Error cannot initialize a data member here
    };
    --------------------------------------------------------------------------

    Then I tried this:
    _________________________________________________________________
    class stu
    {
    const int a;
    };

    int stu::a=10; //Error : Multiple declaration of stu::a
    --------------------------------------------------------------------------

    Then this:
    _________________________________________________________________
    class stu
    {
    const int a;

    stu() //constructor
    {
    a=10; //Error: Cannot modify a const member
    }

    };

    --------------------------------------------------------------------------

    Please help!!!!

    Thanks
    Mr V
     
  2. jcsd
  3. Jul 16, 2007 #2
    I am using Turbo C++ 3.01

    Mr V
     
  4. Jul 16, 2007 #3
    IIRC, when you don't need external linkage you can do this:

    class stu { static const int a=10; };

    If you need external linkage (eg you're building a library that will be linked to other modules) you're safer with this form:

    class stu { static const int a; };
    const int stu::a=10;

    ...I think.
     
  5. Jul 16, 2007 #4

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    You're just missing the 'static' keyword.

    - Warren
     
  6. Jul 16, 2007 #5
    Thanks a lot! It works perfectly.
    But I am a little confused as to why the keyword 'const' is preceded by 'static'. Can't I just create a const without making it a static? I am asking this because value of a const cannot be changed, then why do we need to make it a static?

    Mr V
     
  7. Jul 16, 2007 #6
    Yeah, I realised that. But can you explain why we need to make it a static here?
    Is this to make the objects able to share the const.
    Mr V
     
  8. Jul 16, 2007 #7

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    A static member belongs to the class; it is shared among all instances of the class. A non-static member belongs to a specific instance.

    - Warren
     
  9. Jul 16, 2007 #8
    One more question: if I declare a member function as 'static const' what does it mean? I know that since it is static, it is restricted to accept/access static data members only. But what effect does maknig it a 'const' have on it? Will it accept/access only const values?

    Mr V
     
  10. Jul 16, 2007 #9

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Static and const mean two different things. Static members belong to the class, rather than specific instances of the class. Static data isn't necessarily constant, though. A constant member is one that is created with a specific value, and whose value cannot ever be changed.

    - Warren
     
  11. Jul 16, 2007 #10
    Sure you are right. But does making a function constant restrict it to access only const members?

    Mr V
     
  12. Jul 16, 2007 #11

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    No. If a function is constant, then it returns something (perhaps an object) which cannot be changed by the caller. It doesn't matter what data the function uses to create its return value.

    - Warren
     
  13. Jul 16, 2007 #12
    Oh!Oh! How did I forget that! I forgot that the datatype preceding function name specifies its return type. So, if I declare a function const, it will return a const value.

    Thanks a lot to you, and 'out of whack' for all your help. I hope I would fare well in my exam. Bye then!

    Mr V
     
  14. Jul 16, 2007 #13

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Good luck!

    - Warren
     
  15. Jul 17, 2007 #14
    Thanks!
    I performed well. Only forgot that 'setw' is in 'iomanip.h' and that puts() automatically includes a newline character at the end of a string.
     
  16. Jul 17, 2007 #15
    I have my chemistry paper next, and I have a terrible confusion in Henry's law of solubility. I am creating the thread in the homework section (chemistry). Please help me there...:smile:

    Mr V
     
  17. Jul 17, 2007 #16

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Mr Virtual,

    Those things don't matter anyway. No one uses puts(). I'm almost amazed teachers test on such useless things!

    - Warren
     
  18. Jul 17, 2007 #17
    It matters for me, because there was a question in which we had to write the output of a program, and there was a puts() function in it 8 times, which means there should be 8 lines in the output. But since I forgot this, I wrote the whole output in one line.
     
  19. Jul 17, 2007 #18
    I have just put up the chemistry thread. Please have a look at it...

    Mr V
     
  20. Jul 17, 2007 #19

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Just a note Mr Virtual, please don't advertise one thread in another. Just make a new thread and let people read it as they will.

    - Warren
     
  21. Jul 20, 2007 #20
    Right. I will keep that in mind.

    Mr V
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: C++ How to declare/initialize a constant in a class?
  1. How can is find c? (Replies: 1)

Loading...