initialization of a struct in a vector

Discussion in 'C++' started by Jayw710, Feb 4, 2005.

  1. Jayw710

    Jayw710 Guest

    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
     
    Jayw710, Feb 4, 2005
    #1
    1. Advertising

  2. Jayw710

    Shezan Baig Guest

    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
     
    Shezan Baig, Feb 4, 2005
    #2
    1. Advertising

  3. 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
     
    Andrey Tarasevich, Feb 4, 2005
    #3
  4. Jayw710

    Shezan Baig Guest

    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?
     
    Shezan Baig, Feb 4, 2005
    #4
  5. 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
     
    Andrey Tarasevich, Feb 4, 2005
    #5
  6. Jayw710

    Jayw710 Guest

    >
    >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
     
    Jayw710, Feb 4, 2005
    #6
  7. Jayw710

    Shezan Baig Guest

    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-
     
    Shezan Baig, Feb 4, 2005
    #7
  8. Jayw710

    Guest

    Shez, what does the allocator have to do with the values used to
    initialize concrete data members? Nothing... /david
     
    , Feb 4, 2005
    #8
  9. Jayw710

    Shezan Baig Guest

    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-
     
    Shezan Baig, Feb 4, 2005
    #9
  10. Jayw710

    Real Name Guest

    > 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
     
    Real Name, Feb 4, 2005
    #10
  11. 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
     
    Andrey Tarasevich, Feb 4, 2005
    #11
  12. Jayw710

    Real Name Guest

    "Andrey Tarasevich" <> schrieb im Newsbeitrag
    news:...
    > 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
     
    Real Name, Feb 7, 2005
    #12
  13. 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
     
    Andrey Tarasevich, Feb 7, 2005
    #13
  14. Jayw710

    Real Name Guest

    "Andrey Tarasevich" <> schrieb im Newsbeitrag
    news:...

    > 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.
     
    Real Name, Feb 8, 2005
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pmatos
    Replies:
    6
    Views:
    23,825
  2. Chris Fogelklou
    Replies:
    36
    Views:
    1,391
    Chris Fogelklou
    Apr 20, 2004
  3. Replies:
    8
    Views:
    1,930
    Csaba
    Feb 18, 2006
  4. Replies:
    2
    Views:
    740
    David Harmon
    Sep 20, 2006
  5. Javier
    Replies:
    2
    Views:
    569
    James Kanze
    Sep 4, 2007
Loading...

Share This Page