CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Sep 2003
    Posts
    52

    Member Functions in struct

    hello, gurus,

    I know that we can have member functions defined within a structure (rather than in a class), e.g. member functions can be defined inline, or just as a function pointer and we define the content of the function somewhere else.(Actually, i'm not quite sure about this, but since I really saw some code already doing this, so i think it should work anyway~~)

    So, here is what i want do: first, I define the structure,say struct A, containing some member functions; then I allocate a piece of memory with the sizeof(A)(and fill it with 0s); then I get a pointer to that piece of memory. Now the problem is :What will happen when I cast that pointer to a pointer to struct A, and call:
    pointer->oneFunctionOfA() ??
    will this work? I mean will the system know where to find the content of that oneFunctonOfA() ? if yes, why?

    I know this is a weird question, but I really want to know(since i see some code seems to be doing this kind of thing...).

    thank you in advance

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

    Re: Member Functions in struct

    Originally posted by FredLiu
    hello, gurus,

    I know that we can have member functions defined within a structure (rather than in a class),
    OK
    So, here is what i want do: first, I define the structure,say struct A, containing some member functions; then I allocate a piece of memory with the sizeof(A)(and fill it with 0s); then I get a pointer to that piece of memory. Now the problem is :What will happen when I cast that pointer to a pointer to struct A, and call:
    pointer->oneFunctionOfA() ??
    Undefined behavior.

    When you have a struct or class that has member functions (non-POD type), you no longer can allocate memory and point to it as you would with a POD (Plain-Old-Data) struct or class. You must instantiate the object (either declare an instance, or use operator new to dynamically create an instance).

    The only methods in C++ where you allocate memory for a non-POD type and "point" to is is in the case of something called placement new (which has been discussed in CodeGuru and your C++ book should talk about this). See this link:

    http://www.parashift.com/c++-faq-lit...html#faq-11.10

    Also, there is no difference between a struct and a class except for the default access specifier. By default, a struct has public members, and a class has private members. Also, a struct inherits publicly by default, while a class inherits privately by default. There are no other differences.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Jan 2001
    Posts
    253
    Hi Paul,

    Are you sure about this object not still being a POD?

    I thought that an object can still be a POD if you add non-static non-virtual member functions.

    Best regards,
    John

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449
    Originally posted by jwbarton
    Hi Paul,

    Are you sure about this object not still being a POD?

    I thought that an object can still be a POD if you add non-static non-virtual member functions.

    Best regards,
    John
    Once you add member functions, the class/struct becomes non-POD. For example, adding a constructor or destructor to a class/struct makes it non-POD.

    Also, if you add a member variable that is non-POD, the class/struct becomes non-POD.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Jan 2001
    Posts
    253
    Hi Paul,

    I looked at the answer to the question: What is a "POD type"? at http://www.parashift.com/c++-faq-lit....html#faq-26.7.

    It says that a POD can have static data members, static member functions, and non-static non-virtual member functions.

    It also says that a POD can't have constructors, an assignment operator, virtual functions, base classes, non-static members that are private or protected, or a destructor.

    Is this an accurate definition of a POD, or is the actual definition different?

    Best regards,
    John

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449
    Originally posted by jwbarton
    Hi Paul,

    I looked at the answer to the question: What is a "POD type"? at http://www.parashift.com/c++-faq-lit....html#faq-26.7.

    It says that a POD can have static data members, static member functions, and non-static non-virtual member functions.

    It also says that a POD can't have constructors, an assignment operator, virtual functions, base classes, non-static members that are private or protected, or a destructor.

    Is this an accurate definition of a POD, or is the actual definition different?

    Best regards,
    John
    If the FAQ considers non-static, non-virtual members as POD compatible, I have no argument. I don't see in the ANSI spec where it states exactly that these types of functions are considered POD compatible (maybe I didn't look hard enough).

    However, the problem I see is that it is very easy to make a POD class non-POD, thereby making code that used to work fail miserably (i.e. malloc() to "create" the instance, memcpy() to initialize, etc.). I recently had to fix a problem where C-style functions were used on a structure, but in a later version, the structure had introduced a std::string member, causing crashes to occur in obscure places. The OP should use placement-new if he/she wishes to allocate memory themselves, or create the instance the "C++ way" to avoid having these problems.

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Jan 2004
    Posts
    20

    Re: Re: Member Functions in struct

    Originally posted by Paul McKenzie
    Undefined behavior.

    When you have a struct or class that has member functions (non-POD type), you no longer can allocate memory and point to it as you would with a POD (Plain-Old-Data) struct or class. You must instantiate the object (either declare an instance, or use operator new to dynamically create an instance).
    behavior will depend on the type of object and the type of function you call (as it should be obvious from the previous POD-nonPOD discussion). However, you can allocate memory (via malloc(), calloc(), etc.) and use it later, PROVIDED THAT you put an actual object there using placement new. it's the whole idea behind creating/using custom allocators.

  8. #8
    Join Date
    Sep 2003
    Posts
    52
    Thank you all for your replies~~

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