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?

ClassA::ClassA(const char *db) : ok_flag(false),time_range(false),prefix()
{
//.. Constructor statements
}

plz help
thanx.

Recommended Answers

All 5 Replies

http://cboard.cprogramming.com/showthread.php?t=93997

i also asked this question to that forum, and the thread is going on.

Originally Posted by laserlight
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?

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?

ClassA::ClassA(const char *db) : ok_flag(false),time_range(false),prefix()
{
//.. Constructor statements
}

plz help
thanx.

The part after the colon( : ) is called Initializater List.... if you check on the internet you can find alot info on this, one site you could check is this...

I will attempt to give an explanation but be warned it may not be the best...

Every class has fields that must be initialized when the class is instantiated{to logical initial data}, this is the role of the constructor(ctor). You should also know that when you instantiate a class the constructor of that class calls the {default} constructors of all the parent classes... This was really obvious in java when in the ctor the first line to be executed was super(){either written implicitly or explicitly} that called the parent constructor.

You definately know that a constructor is a special function that has a body, a name and parameter list. One special feature of a constructor is that it also has an initializer list.

initilizer list-->> starts with a colon, and it is followed by comma seperated list of fields initialized with "parentheses"
PS:: the inializer list is speciefied only in the ctor definition not declaration

in the example you specified i've bolded the list

ClassA::ClassA(const char *db) : [B]ok_flag(false),time_range(false),prefix()[/B]
{
//.. Constructor statements
}

The misunderstood part about the initializer list is that they are pointless since you can "initialize" all the members inside the constructor body...but in reality when you use the latter method you don't initialize the member fields but you assign values to them...

To the compiler it doesn't matter if you have defined or not the initializer list, it will initialize the member by its own{when he can--meaning when he can find a default constructor for them}, and then {if you want him to} it will override this initialization with the assignments on the ctor body...

That has an impact on performance{i think} and also it makes it difficult {if not impossible-- i am not sure} to make some of the following tasks::

If you have a const field, then it can be initialized only once, so it must be initialized in the initialization list.

because we already saw that in the ctor body is not an initialization but an assignment

If you have a field that has no default constructor (or a parent class with no default constructor), you must specify which constructor you wish to use.

References
If you have a field that is a reference, you also must initialize it in the initialization list; since references are immutable they can be initialized only once.

One book i read{i don't remember the title} presented the following concept:::

Imagine that a constructor has 2 phases the initialization phase and the "body phase"{all statements in the ctor body}...when you want to initialize a value use the initialization phase a.k.a initialization list

hope it helped!
nicolas

PS:: when i begun posting this i haven't seen the post of salem

link and your explanation helped me.
thank you very much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.