Initializing priority_queue in constructor

R

Ray

Hi all,

After many years of C, I thought I'd move to C++ recently. I think
because I think in C, I'm coming to a misunderstanding of something.

I've created a class foo which contains a private variable which is a
priority queue. In class foo's header file, I declared it as:

class foo {
private:
unsigned int count;
priority_queue<double, std::vector<double>, greater<double> >
pqueue;
};

In the constructor, I'm being warned that I should initialize in the
member initialization list (as I am new, I added all these warnings to
my compiler). So, I know I could do:

foo::foo ()
: count (0)
{
}

but now I'm stuck. How does one initialize a priority queue? I want
to have a priority queue with nothing (i.e., empty) and it's already
done that for me in the declaration (I think...). Did I just get
myself in trouble by turning on the warnings and I should just ignore
this one about initializing pqueue? Obviously, this isn't an error
and I can continue...but I would like to know what should I do...

Thanks!

Ray
 
R

Ray

Hi Alf,

Thank you for your reply! Sorry, but I forgot to clarify that it does
compile fine... But because of the warning flag I'm using (-Weffc++
in g++), I'm getting a warning (and not a compiling error). As I'm
new, I'd like to know whether or not to ignore this warning (i.e.,
what is "best" practice). According to my man pages, -Weffc++ does
this:

-Weffc++ (C++ only)
Warn about violations of the following style guidelines
from Scott
Meyers' Effective C++ book:

* Item 11: Define a copy constructor and an assignment
operator
for classes with dynamically allocated memory.

* Item 12: Prefer initialization to assignment in
constructors.

....

I cut the rest out as it seems obvious it is warning me because of
Item 12. The warning (which I do get with the code you posted
below...thank you for putting it together!) is:

foo.c: In constructor 'foo::foo()':
foo.c:16: warning: 'foo::pqueue' should be initialized in the member
initialization list

So, I guess what I am wondering is can a priority_queue (vector, etc.)
be "initialized" to satisfy this compiler warning or should I just
ignore it?

Thank you!

Ray
 
R

Ray

Sorry, but forgot to add that I am using g++ version 4.1.2 (not sure
if this matters)...

Ray
 
C

Chris Henry

Ray wrote:
[snip]
I've created a class foo which contains a private variable which is a
priority queue. In class foo's header file, I declared it as:

class foo {
private:
unsigned int count;
priority_queue<double, std::vector<double>, greater<double> >
pqueue;
};

In the constructor, I'm being warned that I should initialize in the
member initialization list (as I am new, I added all these warnings to
my compiler). So, I know I could do:

foo::foo ()
: count (0)
{
}

but now I'm stuck. How does one initialize a priority queue? I want
to have a priority queue with nothing (i.e., empty) and it's already
done that for me in the declaration (I think...). Did I just get
[snip]

Have you tried:

foo::foo ()
: pqueue(),
count(0)
{

}

The warning just meant that you should initialized each of the member
variables before entering the constructor body. I only used C++
sparingly, so I hope the code above is correct. What I was trying to
do with the code was to call priority_queue default constructor before
actually entering the constructor body.

Chris
 
R

Ray

Thanks Pete and Chris!

Firstly, yes, pqueue () did the trick. I never thought of it. This
declares foo:

double foo;

and this initializes foo:

foo = 0.0;

so, I was expecting some way of giving pqueue some "initial"
value...or at least, that is what I thought the warning was telling
me. Yet, it's an initial priority queue...so it has nothing -- that's
probably why I was confused.

Thanks for your comment, Chris! I guess I shouldn't be wasting my
time with the warnings, but it seemed like an interesting flag to turn
on for the g++ compiler; and, as a newbie, I was asking myself, "If
this was added as an option to the g++ compiler, it must have some
use..." Sounds like I was wrong. :)

Thanks for your help!

Ray
 
R

Rolf Magnus

Ray said:
Firstly, yes, pqueue () did the trick. I never thought of it. This
declares foo:

double foo;

and this initializes foo:

foo = 0.0;

Actually, it doesn't. It assigns to foo.
so, I was expecting some way of giving pqueue some "initial"
value...or at least, that is what I thought the warning was telling
me. Yet, it's an initial priority queue...so it has nothing -- that's
probably why I was confused.

You could have done the same for the count member:

foo::foo ()
: pqueue(),
count()
{
}

This will initialize count to zero.
Thanks for your comment, Chris! I guess I shouldn't be wasting my
time with the warnings,

Well, you shouldn't ignore warnings that you don't know the reason for.
but it seemed like an interesting flag to turn
on for the g++ compiler; and, as a newbie, I was asking myself, "If
this was added as an option to the g++ compiler, it must have some
use..." Sounds like I was wrong. :)

Not all flags are relevant to everyone. If a warning isn't enabled
with -Wall, -Wextra oder -pedantic, and rather needs to be explicitly
switched on, there must be a reason for that.
 
R

Ray

Hi Rolf,

You could have done the same for the count member:

foo::foo ()
: pqueue(),
count()
{

}

This will initialize count to zero.


Oh...I didn't realize that was possible for a basic data type...

Well, you shouldn't ignore warnings that you don't know the reason for.


True...I'm glad that all of you patiently explained it instead of
telling me to "just turn it off". (I had that worry before I
asked...)

Not all flags are relevant to everyone. If a warning isn't enabled
with -Wall, -Wextra oder -pedantic, and rather needs to be explicitly
switched on, there must be a reason for that.


That's a good point. I will keep that in mind... Thanks!

Ray
 
R

Ray

Hi Pete,

Keep in mind, too, that double, being a bultin type, doesn't get
initialized if you don't provide an initializer.

double foo; // value is indeterminate

priority_queue, on the other hand, has a default constructor, so
creating one without an initializer does initialize it:

priority_queue pqueue; // initialized with default constructor


I see. Is it too bold to question why the -Weffc++ parameter to g++
didn't pick it up, or one can't ask for too much and in the end, it
was just a warning; it wasn't like it stopped me from compiling.

I don't have the book the man pages mentions but I did read the FAQ
about the benefits of initialization lists over assignment in
constructors. So the warning, "Prefer initialization to assignment
in constructors." implies that if you had performed an assignment in a
constructor, it should warn you. But not if you didn't do any
assignment at all (as I had done with pqueue).

Ray
 
C

Chris Henry

I see.  Is it too bold to question why the -Weffc++ parameter to g++
didn't pick it up, or one can't ask for too much and in the end, it
was just a warning; it wasn't like it stopped me from compiling.

I don't have the book the man pages mentions but I did read the FAQ
about the benefits of initialization lists over assignment in
constructors.   So the warning, "Prefer initialization to assignment
in constructors." implies that if you had performed an assignment in a
constructor, it should warn you.  But not if you didn't do any
assignment at all (as I had done with pqueue).
If I did not recall wrongly, the only reason why this was suggested is
because sometime a variable is initialized automatically, at other
time it doesn't (such as builtin types). Therefore by making a
practice of explicitly initializing them, it will save the trouble of
undefined behaviour when you forgot to initialize the variable. Of
course, as Pete says, this also introduces redundant code.

Chris
 
R

Ray

Hi Chris/Pete,

That warning is a compiler implementor's attempt to tell you about his
interpretation of someone's book. You should ask the GNU folks about it.

I see...I will think about that; but I'll probably see if I can find
that section of the book first to read.

Thank you for your help, everyone! I feel better that I understand
the warning; not good if the compiler is smarter than me. :)

Ray
 
C

Chris Henry

I see...I will think about that; but I'll probably see if I can find
that section of the book first to read.
Think I have it somewhere if you need it. I can reproduce the pages
(if I can find the corresponding pages). Drop me an e-mail if you want
it. I'll get it as soon as I could.

Chris
 
C

Charles Coldwell

Pete Becker said:
To initialize foo, give it a value when it's created:

double foo = 0.0; // initialization
foo = 0.0; // assignment

Keep in mind, too, that double, being a bultin type, doesn't get
initialized if you don't provide an initializer.

Apropos this topic (which comes up a lot on this forum), what is the
difference between a "builtin type" and POD ("Plain Ol' Data")? Does
POD include structures composed entirely of builtin types?

Chip
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top