Thread: constructor for POD types?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    8.5 is the relevant standard section. I was inaccurate in my wording above. The accurate but simplified version is this:

    default-initialization: For non-PODs, call the default constructor (if any). For PODs, zero-initialize.

    zero-initialize: For simple variables, set to 0. For aggregates, recursively zero-initialize all members. For non-PODs, set the underlying storage to all 0s.

    T(): Default-initialize.

    T t;: For non-primitives, default-initialize. For primitives, don't initialize at all.

    Note: T t(); declares a function.

    In addition, all static duration variables (globals, class statics, function statics) are zero-initialized before anything else is done.

    Does that clear everything up?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks CornedBee,


    I quote your below two statements here.

    1. "For PODs, zero-initialize."

    2. "T t;: For non-primitives, default-initialize. For primitives, don't initialize at all."

    Are they conflicting with each other for POD type? (I assume when you talks about primitive, you mean POD types.)

    For normal understanding, T t should invoke constructor (default constructor), you mean for POD type, default constructor is not invoked so the value is un-initialized?

    If yes, does any Spec covers T t does not make zero-initialization for POD type?

    Quote Originally Posted by CornedBee View Post
    8.5 is the relevant standard section. I was inaccurate in my wording above. The accurate but simplified version is this:

    default-initialization: For non-PODs, call the default constructor (if any). For PODs, zero-initialize.

    zero-initialize: For simple variables, set to 0. For aggregates, recursively zero-initialize all members. For non-PODs, set the underlying storage to all 0s.

    T(): Default-initialize.

    T t;: For non-primitives, default-initialize. For primitives, don't initialize at all.

    Note: T t(); declares a function.

    In addition, all static duration variables (globals, class statics, function statics) are zero-initialized before anything else is done.

    Does that clear everything up?

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. invalid types 'int[int]' for array subscript
    By kolistivra in forum C++ Programming
    Replies: 6
    Last Post: 12-11-2010, 12:57 PM
  2. Generalising methods to handle multiple weapon types
    By Swarvy in forum Game Programming
    Replies: 2
    Last Post: 05-22-2009, 02:52 AM
  3. Replies: 6
    Last Post: 08-23-2008, 01:16 PM
  4. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM