initialization of a struct in a vector

J

Jayw710

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
 
S

Shezan Baig

Jayw710 said:
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-
 
A

Andrey Tarasevich

Jayw710 said:
...
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.
 
S

Shezan Baig

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

abc::abc() : mypoints(100)


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?
 
A

Andrey Tarasevich

Shezan said:
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.
 
J

Jayw710

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
 
S

Shezan Baig

Jayw710 said:
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-
 
D

davidrubin

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

Shezan Baig

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-
 
R

Real Name

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
 
A

Andrey Tarasevich

Real said:
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.
 
R

Real Name

Andrey Tarasevich said:
'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
 
A

Andrey Tarasevich

Real said:
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.
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
--
Call it as you want.

That's, BTW, is an important detail, not just a matter of "calling it as
someone wants".
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++.
 

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,770
Messages
2,569,586
Members
45,085
Latest member
cryptooseoagencies

Latest Threads

Top