Class in a struct

Hi,
I have two questions.
1. I know it is possible to declare class objects in structs. But is it ethical to do that from design point of view?
2. In my scenario I have a structure with huge number of member elements and I want to include a class object too. Here I have another question. If I memset() the whole struct the class handle is also reset. So, I check the size of the rest of the struct without the class object and subtract it while I call memset(), to get rid os this problem. (Please note that I am using STL class 'queue' here. And I cannot use sizeof() operator on the object, since it is not overloaded.)
But this is totally an unacceptable way to do that. Can you suggest some solution to this problem?
1. Why not? A struct is a class so there is nothing special about them.

2. One solution is to just avoid using memset. Set each member individually.
closed account (zb0S216C)
kokoanant wrote:
"But is it ethical to do that from design point of view?"

It's called "Object Association", and it depends on the design. Typically, object association is called the "Has-A" relationship.

kokoanant wrote:
"If I memset() the whole struct the class handle is also reset."
Do not use memset() on a non-POD type, because you'll trample over the virtual table (if it has one)

kokoanant wrote:
"And I cannot use sizeof() operator on the object, since it is not overloaded.) "

You cannot overload the sizeof() operator anyway, because the compiler requires its use internally, and overloading would break allocation (both stack and heap) requests. And yes, you can use sizeof() on:

- Expressions that yield a value
- Expressions that yield a type

Wazzak
Agree with Peter87. Setting each member individually seems not so 'cool' as memset() but it's good for readability and portal and easy of maintainance.
Topic archived. No new replies allowed.