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
Re: Member Functions in struct
Quote:
Originally posted by FredLiu
hello, gurus,
I know that we can have member functions defined within a structure (rather than in a class),
OK
Quote:
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
Re: Re: Member Functions in struct
Quote:
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.