Board index » Visual Studio » initialize a struct

initialize a struct

Visual Studio100
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...

Hope you understand

Thx in adv

Jack


-
 

Re:initialize a struct

Sorry if there is any wrong terminology.... :)

Thanks

Jack



"Jack" <jl@knight.com>¼¶¼g©ó¶l¥ó·s»D:eK$NpN6vHHA.4592@TK2MSFTNGP05.phx.gbl...

Quote
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...

Hope you understand

Thx in adv

Jack









-

Re:initialize a struct

Hi Jack,



Quote
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.



If you want to enhance your existing structs you could e.g. publicly inherit

or privately contain your old struct in a new struct or class to give that

constructor/destructor, copy semantics and so on.



--

SvenC



-

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





-