Thread: What does these lines do after ":" mark?

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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-21-2008, 12:58 AM
  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, 11:31 PM