473,386 Members | 1,763 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 4133
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Dave | last post by:
Hello all, I'm pondering why the default underlying container for std::priority_queue<> is std::vector<>. It would seem that inserts are liable to happen anywhere, which would make std::list<>...
3
by: Tino | last post by:
In using std::priority_queue, I'm concerned about the expense of memory allocation and copying as the priority_queue grows large. std::vector has reserve() to address this concern, though there...
3
by: zl2k | last post by:
hi, all Here is what I want to do: to wrap my self defined class in a shared_ptr and then insert it into the priority_queue. The priority_queue should pump the least element of the self defined...
9
by: Henning Hasemann | last post by:
I'm using a stl-priority queue and want - find out if a certain item is contained in the queue - to be able iterate over all items without having to pop() them, order does not matter. I couldnt...
18
by: J.M. | last post by:
I would like to use a data structure (e.g. from the STL) that always allows me to retrieve the largest element. (I want to push in elements, and remove the largest, push in further elements, etc.)...
6
by: Eric Lilja | last post by:
Hello, I have a the following priority_queue: priority_queue<pair<int, string pq; AFAICT, priority_queues doesn't support iterators. My question: is there a way to print its contents without...
10
by: Jason Doucette | last post by:
Situation: I have a simple struct that, say, holds a color (R, G, and B). I created my own constructors to ease its creation. As a result, I lose the default constructor. I dislike this, but...
8
by: thomas | last post by:
priority_queue usually uses the greater<intpredicate function. But as you know, we don't always use priority_queue<int>. Actually we may need the "priority_queue<pair<int,int>,...
24
by: Joe, G.I. | last post by:
Can anyone help me w/ a priority_queue. I'm generating MyEvent classes and I put them on a priority_queue, but I don't know how to get them in priority. The priority is the event w/ the smallest...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.