Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > initialization of a struct in a vector

Reply
Thread Tools

initialization of a struct in a vector

 
 
Jayw710
Guest
Posts: n/a
 
      02-04-2005
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


 
Reply With Quote
 
 
 
 
Shezan Baig
Guest
Posts: n/a
 
      02-04-2005

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


 
Reply With Quote
 
 
 
 
Andrey Tarasevich
Guest
Posts: n/a
 
      02-04-2005
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
 
Reply With Quote
 
Shezan Baig
Guest
Posts: n/a
 
      02-04-2005

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?

 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      02-04-2005
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

 
Reply With Quote
 
Jayw710
Guest
Posts: n/a
 
      02-04-2005
>
>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

 
Reply With Quote
 
Shezan Baig
Guest
Posts: n/a
 
      02-04-2005

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-

 
Reply With Quote
 
davidrubin@warpmail.net
Guest
Posts: n/a
 
      02-04-2005
Shez, what does the allocator have to do with the values used to
initialize concrete data members? Nothing... /david

 
Reply With Quote
 
Shezan Baig
Guest
Posts: n/a
 
      02-04-2005

http://www.velocityreviews.com/forums/(E-Mail Removed) 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-

 
Reply With Quote
 
Real Name
Guest
Posts: n/a
 
      02-04-2005
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
Initializing vector<vector<int> > and other vector questions... pmatos C++ 6 04-26-2007 05:39 PM
static struct initialization in a Class:: -- not my struct christian.bongiorno@gmail.com C++ 2 09-20-2006 06:53 PM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM



Advertisments