Initialize const variables in constructors

This is a discussion on Initialize const variables in constructors within the C++ Programming forums, part of the General Programming Boards category; Is it possible to initialize a private const member variables in a constructor?...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    2

    Initialize const variables in constructors

    Is it possible to initialize a private const member variables in a constructor?

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Use an initializer list:
    Code:
    class Foo
    {
    public:
      Foo(int num):FooInt(num){}
    private:
      const int FooInt;
    };
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The so annoying LNK2005... please help!
    By Mikey_S in forum C++ Programming
    Replies: 14
    Last Post: 02-01-2009, 03:22 AM
  2. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  3. Initialize const member variables in constructors
    By mvgian in forum C++ Programming
    Replies: 5
    Last Post: 03-21-2006, 07:23 AM
  4. Debugging help
    By cuddlez.ini in forum C++ Programming
    Replies: 3
    Last Post: 10-24-2004, 07:08 PM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21