CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 21 of 21

Thread: Destruction???

  1. #16
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Question Re: Destruction???

    Thanks Paul for explaining the undefined behaviour. However, I was not quite sure what a POD type meant. I looked for its definition and here I quote it.
    A POD type is a C++ type that has an equivalent in C, and that uses the same rules as C uses for initialization, copying, layout, and addressing.

    As an example, the C declaration struct Fred x; does not initialize the members of the Fred variable x. To make this same behavior happen in C++, Fred would need to not have any constructors. Similarly to make the C++ version of copying the same as the C version, the C++ Fred must not have overloaded the assignment operator. To make sure the other rules match, the C++ version must not have virtual functions, base classes, non-static members that are private or protected, or a destructor. It can, however, have static data members, static member functions, and non-static non-virtual member functions.

    The actual definition of a POD type is recursive and gets a little gnarly. Here's a slightly simplified definition of POD: a POD type's non-static data members must be public and can be of any of these types: bool, any numeric type including the various char variants, any enumeration type, any data-pointer type (that is, any type convertible to void*), any pointer-to-function type, or any POD type, including arrays of any of these. Note: data-pointers and pointers-to-function are okay, but pointers-to-member are not. Also note that references are not allowed. In addition, a POD type can't have constructors, virtual functions, base classes, or an overloaded assignment operator.
    The source of this definition is - C++ FAQ Lite - Marshall Cline.

    So how do I ensure that my class is a POD type? That would mean that I would have to ensure that the object creation, copying, assignment are done by explicit non-virtual member functions (because in a C++ class we are by default provided with the 2 constructos) which seems to be a very wierd (and rare) thing. In that case I suppose we are restricted not to use classes instead we need to use C-type structs at most, in C++. So POD simply means the datatypes supported by C and hence I can conclude that when you are writing variadic functions in C++, its actually a C-code, else it would be an undefined behaviour. I am not sure if I am understanding the POD concept in relation to C++ classes that well.
    Last edited by exterminator; August 17th, 2005 at 04:38 AM.

  2. #17
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Destruction???

    The definition is correct. It basically means that a class or struct must must be 'C' compatible to be POD. For C++ specfic POD types, you throw in static members and member functions into the mix. There really isn't much more to say beyond what the definition in the C++ FAQ states.

    The one thing that a lot of programmers do (usually C programmers who are beginners using C++) is to use non-POD types as if they are POD types. The usage of them in printf() statements is just one mistake.

    Basically, if you have a non-POD type, you must use C++ (not C) techniques to manipulate it. This means constructing it properly (not use malloc()), destroying it correctly, copying it using the appropriate operator = (not use memcpy()), use std::sort() to sort an array of non-POD types. Some other examples -- you can't use macros such as "offsetof()" on non-POD types, you can't use a non-POD type as the address of a buffer in an fwrite() call, etc.

    Regards,

    Paul McKenzie

  3. #18
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Thumbs up Re: Destruction???

    Thanks again Paul for the clarifications.

    I was just going through an article that tells about the variadic functions and their alternatives in C++ that are:
    1. Functions with Default Arguments
    2. USage of Function Templates
    3. Packing Function Arguments in a Container
    4. Overloading operator <<
    Here's the link if OPer wishes to go through the article - Informit. I hope all the concerns of sszd have been nicely addressed as are mine . Nice thread.

  4. #19
    Join Date
    Feb 2005
    Location
    Denver
    Posts
    353

    Re: Destruction???

    I would like to thank both Paul and exterminator for their time and input into this subject. And yes, I am quite satisified with the explanation.

    I do have one additional question. Is there a site where one can read the actual ANSI standard documents? I've done some searching and can find nothing except some sites where you must pay to become a member before you are allowed to even browse the documents they have.

    Thanks again.

  5. #20
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: Destruction???

    Here are the final drafts of ISO 98 C++ standard:
    http://library.n0i.net/programming/c/cp-iso/
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  6. #21
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: Destruction???

    The actual standard only costs about $18 and is worth every penny. You can get it from here.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured