By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,240 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

initialization of a struct in a vector

P: n/a
hi all,

this should be basic, but I can't seem to get the right syntax.

i have a struct

struct PTStruct
{
int x;
int y;
};

class abc
{
public:
abc();
...

std::vector<PTStruct> mypoints;
}

void abc::abc() : mypoints(100)
{

}
and i'm trying to get all the PTStruct variables (x and y) in mypoints to be
initialized to zero, but I'm screwing up the syntax somehow.

Any suggestions how this should be done?

Thanks to all!

Jay
Jul 23 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a

Jayw710 wrote:
hi all,

this should be basic, but I can't seem to get the right syntax.

i have a struct

struct PTStruct
{
int x;
int y;
add a ctor here. Ctors are always useful, even in structs:

PTStruct(int initX, int initY) : x(initX), y(initY) { }

};

class abc
{
public:
abc();
...

std::vector<PTStruct> mypoints;
}

void abc::abc() : mypoints(100)

change this to:

void abc::abc() : mypoints(100, PTStruct(0, 0))

Hope this helps,
-shez-
{

}
and i'm trying to get all the PTStruct variables (x and y) in mypoints to be initialized to zero, but I'm screwing up the syntax somehow.

Any suggestions how this should be done?

Thanks to all!

Jay


Jul 23 '05 #2

P: n/a
Jayw710 wrote:
...
i have a struct

struct PTStruct
{
int x;
int y;
};

class abc
{
public:
abc();
...

std::vector<PTStruct> mypoints;
}

void abc::abc() : mypoints(100)
'void'??? Constructors have no return type. Should be just

abc::abc() : mypoints(100)
{

}
and i'm trying to get all the PTStruct variables (x and y) in mypoints to be
initialized to zero, but I'm screwing up the syntax somehow.

Any suggestions how this should be done?
...


You don't need to do anything. The code above already initializes all
field in all structs to zero. If your compiler doesn't do that, it's broken.

--
Best regards,
Andrey Tarasevich
Jul 23 '05 #3

P: n/a

Andrey Tarasevich wrote:
Jayw710 wrote:
...
i have a struct

struct PTStruct
{
int x;
int y;
};

class abc
{
public:
abc();
...

std::vector<PTStruct> mypoints;
}

void abc::abc() : mypoints(100)
'void'??? Constructors have no return type. Should be just

abc::abc() : mypoints(100)
{

}
and i'm trying to get all the PTStruct variables (x and y) in mypoints to be initialized to zero, but I'm screwing up the syntax somehow.

Any suggestions how this should be done?
...


You don't need to do anything. The code above already initializes all
field in all structs to zero. If your compiler doesn't do that, it's

broken.


Doesn't it depend where the vector gets its memory from?

Jul 23 '05 #4

P: n/a
Shezan Baig wrote:
Andrey Tarasevich wrote:
Jayw710 wrote:
> ...
> i have a struct
>
> struct PTStruct
> {
> int x;
> int y;
> };
>
> class abc
> {
> public:
> abc();
> ...
>
> std::vector<PTStruct> mypoints;
> }
>
> void abc::abc() : mypoints(100)


'void'??? Constructors have no return type. Should be just

abc::abc() : mypoints(100)
> {
>
> }
>
>
> and i'm trying to get all the PTStruct variables (x and y) in mypoints to be > initialized to zero, but I'm screwing up the syntax somehow.
>
> Any suggestions how this should be done?
> ...


You don't need to do anything. The code above already initializes all
field in all structs to zero. If your compiler doesn't do that, it's

broken.

Doesn't it depend where the vector gets its memory from?


No. Why?

Initializing this vector as 'mypoints(100)' is equivalent to
initializing it as 'mypoints(100, PTStruct(),
std::allocator<PTStruct>())' (default arguments get implicitly
filled-in), which means that all freshly created elements are
copy-initialized from 'PTStruct()'. And 'PTStruct()' is an instance of
'PTStruct' with both fields set to 0.

--
Best regards,
Andrey Tarasevich

Jul 23 '05 #5

P: n/a
>
add a ctor here. Ctors are always useful, even in structs:

PTStruct(int initX, int initY) : x(initX), y(initY) { }


Thank you, this is what I was missing. I knew I had left something out (my
compiler VC++ doesn't initialize anything to zero in the structs).

Thanks again!

Jay

Jul 23 '05 #6

P: n/a

Jayw710 wrote:

add a ctor here. Ctors are always useful, even in structs:

PTStruct(int initX, int initY) : x(initX), y(initY) { }

Thank you, this is what I was missing. I knew I had left something

out (my compiler VC++ doesn't initialize anything to zero in the structs).

Thanks again!

Jay


Also you might want to set default arguments, like this:

PTStruct(int initX = 0, int initY = 0) : x(initX), y(initY) { }

And you can simplify your containers constructor to how it was
originally:

abc::abc() : mypoints(100) { }

That way, even if you don't use the standard allocator, it will always
initialize to zero.

Hope this helps,
-shez-

Jul 23 '05 #7

P: n/a
Shez, what does the allocator have to do with the values used to
initialize concrete data members? Nothing... /david

Jul 23 '05 #8

P: n/a

da********@warpmail.net wrote:
Shez, what does the allocator have to do with the values used to
initialize concrete data members? Nothing... /david


Yes, you are right. Sorry, for some reason, my mind was on dynamic
memory :)

-shez-

Jul 23 '05 #9

P: n/a
> And 'PTStruct()' is an instance of
'PTStruct' with both fields set to 0.


Why? PTStruct only contains PODs - their default ctors wont we called
implicit, you have to call them explicit in your own ctor.

regards,
me
Jul 23 '05 #10

P: n/a
Real Name wrote:
And 'PTStruct()' is an instance of
'PTStruct' with both fields set to 0.


Why? PTStruct only contains PODs - their default ctors wont we called
implicit, you have to call them explicit in your own ctor.
...


'PTStruct' is a POD type. Initializer '()' for a POD type causes default
initialization being applied to all its members. Default initialization
for type 'int' is zero initialization. That's all.

BTW, it has nothing to do with any "constructors". Default
initialization process for POD types does not involve constructors.
Moreover, objects of type 'int' have no constructors at all, default or
not.

--
Best regards,
Andrey Tarasevich
Jul 23 '05 #11

P: n/a

"Andrey Tarasevich" <an**************@hotmail.com> schrieb im Newsbeitrag
news:11*************@news.supernews.com...
Real Name wrote:
And 'PTStruct()' is an instance of
'PTStruct' with both fields set to 0.
Why? PTStruct only contains PODs - their default ctors wont we called
implicit, you have to call them explicit in your own ctor.
...


'PTStruct' is a POD type.


Its a struct/class of PODs - it not a POD itself.
Initializer '()' for a POD type causes default
initialization being applied to all its members. Default initialization
for type 'int' is zero initialization. That's all.
Ack. But has nothing to do with PTStruct(): the members wont be initialized.
If you have any other information please point me to the relevant part of
the standard.
BTW, it has nothing to do with any "constructors". Default
initialization process for POD types does not involve constructors.
Call it as you want.
Moreover, objects of type 'int' have no constructors at all, default or
not.


So you think we are unable to construct an integer?

regards,
me
Jul 23 '05 #12

P: n/a
Real Name wrote:
>> And 'PTStruct()' is an instance of
>> 'PTStruct' with both fields set to 0.
>
> Why? PTStruct only contains PODs - their default ctors wont we called
> implicit, you have to call them explicit in your own ctor.
> ...
'PTStruct' is a POD type.


Its a struct/class of PODs - it not a POD itself.


Huh? It _is_ a POD itself. You probably need to look up a definition of
POD type. See 3.9/10 in the standard.
Initializer '()' for a POD type causes default
initialization being applied to all its members. Default initialization
for type 'int' is zero initialization. That's all.


Ack. But has nothing to do with PTStruct(): the members wont be initialized.


Not true. See below.
If you have any other information please point me to the relevant part of
the standard.


See 8.5/7:

--
8.5/7
An object whose initializer is an empty set of parentheses, i.e. (),
shall be default-initialized.
--

Then 8.5/5 to find out what "default-initialized" means in this case of
POD-type:

--
8.5/5
[...]
- otherwise, the storage for the object is zero-initialized
--
BTW, it has nothing to do with any "constructors". Default
initialization process for POD types does not involve constructors.


Call it as you want.


That's, BTW, is an important detail, not just a matter of "calling it as
someone wants".
Moreover, objects of type 'int' have no constructors at all, default or
not.


So you think we are unable to construct an integer?
...


"Construct"? No. You are unable to construct an integer. Only objects of
class types can be constructed. In the standard C++ terminology the
notion of "construction" is not applicable to non-class types.
Apparently, you are mixing the concept of "construction" with more
general concept of "initialization". These are not the same in C++.

--
Best regards,
Andrey Tarasevich
Jul 23 '05 #13

P: n/a

"Andrey Tarasevich" <an**************@hotmail.com> schrieb im Newsbeitrag
news:11*************@news.supernews.com...
Huh? It _is_ a POD itself. You probably need to look up a definition of
POD type. See 3.9/10 in the standard.
Thx - that helped a lot.
Ack. But has nothing to do with PTStruct(): the members wont be initialized.
Not true. See below.


I have not seen any compiler doing so...
If you have any other information please point me to the relevant part of the standard.


See 8.5/7:


Thx again.

Jul 23 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.