Re:initialize a struct
SvenC wrote:
Quote
Hi Jack,
>If I need to initialize a struct (not in the form of memset),
>do I need to do it in the constructor?
>But will the compiler accept a constructor in a typedef struct? or
>plain vanilla struct...
>I am looking for some sort of managed interface for my "structs" just
>like std::string s; doesn't need to be initalized
>and also s.clear(); can clean up what was there before...
In C++ a struct has the same features as a class. The only difference
is that the default access is public not private.
So yes, you can have constructors, a destructor and any other methods
you need.
Just keep in mind that a "plain" struct is [most likely] a POD class,
whereas if you add a constructor, it stops being POD. In many cases
(and for most people) it doesn't matter. The difference is not that
significant, after all. The effects of using 'memcpy' are not defined
for non-POD classes, 'offsetof' is not defined for non-POD and maybe
the most important is a way their members are initialised.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
-