Need help

R

ravi

Hi,
I am new to C++ .Can anyone please tell me what does ":" operator
mean in C++.For ex: there is a program snippet which looks like that

class convert {
protected:
double val1; // initial value
double val2; // converted value
public:
convert(double i) {
val1 = i;
}
virtual void compute() = 0;
};
// Liters to gallons.
class l_to_g : public convert {
public:
l_to_g(double i) : convert(i) { }
void compute() {
val2 = val1 / 3.7854;
}
};

In the above piece of code there is a line as
l_to_g(double i) : convert(i) { }

Can anybody tell me wht does the above line mean

Thanks
Ravi
 
M

Marco Wahl

ravi said:
I am new to C++ .Can anyone please tell me what does ":" operator
mean in C++.For ex: there is a program snippet which looks like that

":" is merely a seperator. Your question is basic and
should be answered in C++-learning books.

Anyway, after reindenting and commenting some your code I come to the
following code:

class convert {
protected:
double val1; // initial value
double val2; // converted value
public:
convert(double i) {
val1 = i;
}
virtual void compute() = 0;
};

class l_to_g : // Seperator for the baseclass-list,
// here class convert.
public convert {
public:
l_to_g(double i) : // After this seperator follows
// the initializer-list,
// e.g. initialize the base-class
// convert.
convert(i) { }
void compute() {
val2 = val1 / 3.7854;
}
};
In the above piece of code there is a line as
l_to_g(double i) : convert(i) { }

Can anybody tell me wht does the above line mean

Hopefully the comments in the code above help you.


Best wishes
 
D

David Harmon

On 29 Aug 2006 23:39:21 -0700 in comp.lang.c++, "ravi"
In the above piece of code there is a line as
l_to_g(double i) : convert(i) { }

Can anybody tell me wht does the above line mean

This particular ":" is an example of the "constructor initializer
list". It passes information to the constructors of members or (as
in this example) base classes that are created _before_ the
constructor body runs.

This issue is mentioned in Marshall Cline's C++ FAQ. See the topic
"[10.6] Should my constructors use "initialization lists" or
"assignment". It is always good to check the FAQ before posting.
You can get the FAQ at:
http://www.parashift.com/c++-faq-lite/

Please see also the topic "[5.7] What makes a good Subject: line?"
 
B

Bart

ravi wrote:
In the above piece of code there is a line as
l_to_g(double i) : convert(i) { }

Can anybody tell me wht does the above line mean

This is a constructor initializer list. It initializes the members and
base clases. Normally, when you have a base class with a default
constructor you can just omit this initializer. For example:

class Base
{
public:
Base(); // default constructor
};

class Derived : public Base
{
public:
Derived() {} // Don't need to initialize the base class. Default
constructor available.
};

But when there is no default constructor you need to explicitly
initialize any base class without one. You also need to explicitly
initialize members that need an initializer, like references and const
members. You can separate the initializers with commas, like so:

MyClassConstructor()
: member1(/* something */)
, member2(/* something */)
, member3(/* something */)
{
}

Generally, it is better form to initialize your members in the
initializer list rather than using assignments inside the body of the
constructor.

Regards,
Bart.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top