What does these lines do after ":" mark?

This is a discussion on What does these lines do after ":" mark? within the C++ Programming forums, part of the General Programming Boards category; i'm trying to read and understand a .cc (c++)file, i want to know what does ok_flag(false),time_range(false),prefix() do after ":" ? ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    5

    What does these lines do after ":" mark?

    i'm trying to read and understand a .cc (c++)file,
    i want to know what does ok_flag(false),time_range(false),prefix() do after ":" ?
    I did know that ":" was being used for class inheritance but what is ":" for in the bleow code?
    Code:
    ClassA::ClassA(const char *db) : ok_flag(false),time_range(false),prefix()
    {
    //.. Constructor statements
    }
    plz help
    thanx.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,733
    They mark the beginning of the initialisation list. The ok_flag and time_range member variables are initialised to false, while the prefix member variable is initialised using its default constructor.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    5
    Quote Originally Posted by laserlight View Post
    They mark the beginning of the initialisation list. The ok_flag and time_range member variables are initialised to false, while the prefix member variable is initialised using its default constructor.
    ok_flag and time_range are the variables, and prefix is a class.

    did i understand correctly?

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    For all we know, ok_flag, time_range and prefix are data members of the class ClassA. We can assume that the first two might be bools, but we have know idea what type prefix might be (int, char, std::string, custom class) unless we go and look it up in the class declaration where you might hope to see something like:

    Code:
    class ClassA
    {
        //...
        bool ok_flag;
        bool time_range;
        SomeType prefix;
    };
    The initialisation list is made up of "constructor calls" for the data members of the class to initialize these members. Built-in types, such as ints and bools, are not really classes and hence do not have a real constructor, but do accept a constructor-like syntax for this purpose.

    If there is nothing between the parenthesis, it means using the default constructor of the type (one that takes no arguments). If prefix happened to be an int, this should set its value to 0 (but I'd prefer to be explicit - prefix(0)). If it happens to be a std::string, it would initialize prefix to be an empty std::string. Etc.
    Last edited by anon; 09-27-2007 at 03:37 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    2
    the post has been answered here

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    5
    Quote Originally Posted by anon View Post
    For all we know, ok_flag, time_range and prefix are data members of the class ClassA. We can assume that the first two might be bools, but we have know idea what type prefix might be (int, char, std::string, custom class) unless we go and look it up in the class declaration where you might hope to see something like:

    Code:
    class ClassA
    {
        //...
        bool ok_flag;
        bool time_range;
        SomeType prefix;
    };
    The initialisation list is made up of "constructor calls" for the data members of the class to initialize these members. Built-in types, such as ints and bools, are not really classes and hence do not have a real constructor, but do accept a constructor-like syntax for this purpose.

    If there is nothing between the parenthesis, it means using the default constructor of the type (one that takes no arguments). If prefix happened to be an int, this should set its value to 0 (but I'd prefer to be explicit - prefix(0)). If it happens to be a std::string, it would initialize prefix to be an empty std::string. Etc.

    so you say ok_flag and time_range are boolean members(variables) of some base class, and prefix is another member(variable) of some base class. if they are not the members of base class why not assigning them in
    Code:
    Class A::Class A(const char *db)
    {
    //Constructor statements
    ok_flag=true;
    time_range=true;
    //etc.
    }
    section?

    another question is about assignment in c++ :
    does "int a = 5;" also means "a(5)" in c++?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,733
    so you say ok_flag and time_range are boolean members(variables) of some base class, and prefix is another member(variable) of some base class. if they are not the members of base class why not assigning them in
    They are members of the class. The term "base" is unnecessary and potentially misleading here as we are not talking about inheritance. Your example assigns values to the member variables. Assignment and initialisation are not the same thing. When the constructor body is entered, the member variables have been initialised, so assignment when you want initialisation may mean extra work. Furthermore, members that are references or const must be initialised using an initialisation list.

    another question is about assignment in c++ :
    does "int a = 5;" also means "a(5)" in c++?
    Code:
    int a = 5;
    and
    Code:
    int a(5);
    are equivalent. Both are examples of initialisation, not assignment.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jul 2007
    Posts
    5
    ok, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  2. Print out first N lines
    By YoYayYo in forum C Programming
    Replies: 1
    Last Post: 02-20-2008, 11:58 PM
  3. Line Counting
    By 00Sven in forum C Programming
    Replies: 26
    Last Post: 04-02-2006, 08:59 PM
  4. count only lines of code...
    By flightsimdude in forum C Programming
    Replies: 13
    Last Post: 09-23-2003, 07:08 PM
  5. try your heads on this
    By jasrajva in forum A Brief History of Cprogramming.com
    Replies: 70
    Last Post: 11-08-2001, 10:31 PM

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