Re: initializer list of the struct

Discussion in 'C++' started by Richard, Oct 20, 2009.

  1. Richard

    Richard Guest

    [Please do not mail me a copy of your followup]

    Johnson <> spake the secret code
    <hbiru3$2c85$> thusly:

    >I would like to know if I initialize a struct, will the constructor
    >function of the struct be called? Actually I found it didn't. I also get
    >an error when I compile it.
    >error C2552: 'perm_address' : non-aggregates cannot be initialized with
    >initializer list
    >
    >Any recommendation to remove the error?
    >
    >Thank you!
    >
    >For example:
    >struct address
    >{
    > int street_no;
    > char *street_name;
    >
    > address (int street_no_,
    > char *street_name_) //it is not called by the initializer
    > {
    > street_no = street_no_;
    > street_name = new char (sizeof (street_name_)/sizeof(char));
    > strcpy (street_name, street_name_);
    > };
    >
    > ~address(void) { delete [] street_name; };
    >
    >};
    >
    >static struct address perm_address =
    > { 3, "Savona Dr."}; //initize the struct


    Use this instead:

    static struct address perm_address(3, "Savona Dr."); //initize the struct

    Either its a dumb struct and takes an aggregate initializer list
    (which is error prone), or its a class with a constructor,
    destructor and methods and you use the c'tor to construct/initialize
    it.
    --
    "The Direct3D Graphics Pipeline" -- DirectX 9 draft available for download
    <http://legalizeadulthood.wordpress.com/the-direct3d-graphics-pipeline/>

    Legalize Adulthood! <http://legalizeadulthood.wordpress.com>
    Richard, Oct 20, 2009
    #1
    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. M Kumar

    struct initializer { 0 }

    M Kumar, Sep 28, 2003, in forum: C Programming
    Replies:
    2
    Views:
    397
    Ivan Vecerina
    Sep 28, 2003
  2. Chris Fogelklou
    Replies:
    36
    Views:
    1,373
    Chris Fogelklou
    Apr 20, 2004
  3. Laurent Deniau

    struct initializer efficiency

    Laurent Deniau, Aug 28, 2007, in forum: C Programming
    Replies:
    0
    Views:
    267
    Laurent Deniau
    Aug 28, 2007
  4. Thomas J. Gritzan

    Re: initializer list of the struct

    Thomas J. Gritzan, Oct 20, 2009, in forum: C++
    Replies:
    0
    Views:
    410
    Thomas J. Gritzan
    Oct 20, 2009
  5. xmllmx
    Replies:
    4
    Views:
    229
    Zhihao Yuan
    Dec 17, 2012
Loading...

Share This Page