468,746 Members | 1,855 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,746 developers. It's quick & easy.

Initializing priority_queue in constructor

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

Jun 27 '08 #1
16 3702
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


On Apr 26, 9:18 pm, "Alf P. Steinbach" <al...@start.nowrote:
It's a good idea to post a minimal complete example.

As it is it's impossible to say what you're doing wrong.

The following compiles fine:

<code>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

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

};

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

}

int main()
{
foo o;}

</code>
Jun 27 '08 #2
Ray

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

Ray

On Apr 26, 9:50 pm, Ray <rayky...@gmail.comwrote:
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:
Jun 27 '08 #3

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
Jun 27 '08 #4
On 2008-04-26 09:42:39 -0400, Chris Henry <ch***********@gmail.comsaid:
>
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.
Welcome to the wonderful world of "mother may I" programming. You're
right, pqueue() explicitly initializes the priority_queue member
object. But that's exactly what the compiller would do if you didn't
tell it to. While some people think it's a good idea to write redundant
code, it's a better use of your time to work on things that the
compiler won't do for you.

Some warnings are just noise.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jun 27 '08 #5
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

On Apr 26, 11:05 pm, Pete Becker <p...@versatilecoding.comwrote:
On 2008-04-26 09:42:39 -0400, Chris Henry <chrishenry...@gmail.comsaid:
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.

Welcome to the wonderful world of "mother may I" programming. You're
right, pqueue() explicitly initializes the priority_queue member
object. But that's exactly what the compiller would do if you didn't
tell it to. While some people think it's a good idea to write redundant
code, it's a better use of your time to work on things that the
compiler won't do for you.

Some warnings are just noise.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)
Jun 27 '08 #6
Ray wrote:
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.

Jun 27 '08 #7
On 2008-04-26 11:09:41 -0400, Ray <ra******@gmail.comsaid:
>

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

double foo;

and this initializes foo:

foo = 0.0;
That assigns a value to foo. 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.

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

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jun 27 '08 #8
On 2008-04-26 12:21:13 -0400, Pete Becker <pe**@versatilecoding.comsaid:
On 2008-04-26 11:09:41 -0400, Ray <ra******@gmail.comsaid:
>>

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

double foo;

and this initializes foo:

foo = 0.0;

That assigns a value to foo. 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.

double foo; // value is indeterminate
Sorry, these code examples are assumed to be inside a function. Outside
a function, builtin types get initialized to all zeros.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jun 27 '08 #9
Ray

Hi Rolf,

On Apr 27, 12:41 am, Rolf Magnus <ramag...@t-online.dewrote:
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...

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.

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...)

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.

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

Ray
Jun 27 '08 #10
Ray
Hi Pete,

On Apr 27, 1:21 am, Pete Becker <p...@versatilecoding.comwrote:
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

Jun 27 '08 #11
On Apr 27, 10:34*am, Ray <rayky...@gmail.comwrote:
On Apr 27, 1:21 am, Pete Becker <p...@versatilecoding.comwrote:
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).
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.
>
Ray
Chris
Jun 27 '08 #12
On 2008-04-26 22:34:00 -0400, Ray <ra******@gmail.comsaid:
>
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.
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.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jun 27 '08 #13
Ray

Hi Chris/Pete,

On Apr 27, 7:38 pm, Pete Becker <p...@versatilecoding.comwrote:
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

Jun 27 '08 #14
On Apr 28, 12:08*pm, Ray <rayky...@gmail.comwrote:
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
Jun 27 '08 #15
Pete Becker <pe**@versatilecoding.comwrites:
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

--
Charles M. "Chip" Coldwell
"Turn on, log in, tune out"
GPG Key ID: 852E052F
GPG Key Fingerprint: 77E5 2B51 4907 F08A 7E92 DE80 AFA9 9A8F 852E 052F
Jun 27 '08 #16
On 2008-04-28 10:26:30 -0400, Charles Coldwell <co******@gmail.comsaid:
Pete Becker <pe**@versatilecoding.comwrites:
>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?
Essentially, a POD is something that would act the same in C and C++.
So no static members, no non-trivial constructors, etc.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jun 27 '08 #17

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Dave | last post: by
3 posts views Thread by Tino | last post: by
9 posts views Thread by Henning Hasemann | last post: by
18 posts views Thread by J.M. | last post: by
6 posts views Thread by Eric Lilja | last post: by
8 posts views Thread by thomas | last post: by
24 posts views Thread by Joe, G.I. | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
xarzu
2 posts views Thread by xarzu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.