Re: Initializing a class field of a custom struct type?
"Bob Rock" <yet_another_apprentice@hotmail.com.nospam> wrote in message
news:ukG2xM%235HHA.2312@TK2MSFTNGP06.phx.gbl...
I also tried it with VS 8.0 and I confirm it does not work.
Abdo are you sure you are doing what we are discussing? I required I may
post a VC++ project that will prove what I'm saying.
No need for a project. The following works:
<code>
#include <iostream>
struct Foobar
{
int i,j;
};
int main ()
{
Foobar foo = {0};
std::cout << foo.i << ", " << foo.j << std::endl; // Prints 0, 0
return 0;
}
</code>
Now although this is not what you want (a "class field of a custom struct
type"), this proves that structures can be "initialized" this way.
As Alex has already mentioned in the contstructor the class fields would be
already initialized so setting their value is assigning not initializing.
If you still want to do this you can do the following:
<TestedCode>
struct MyStruct
{
int i,j;
static MyStruct Zero;
};
MyStruct MyStruct::Zero = {0};
class MyClass
{
MyStruct myStruct;
public:
MyClass() : myStruct(MyStruct::Zero)
{
}
};
</TestedCode>
BTW, why are you so against memset()? Unlike ZeroMemory() memset() is not an
API, it's from CRT.
So unless you have virtual methods in your structure, I don't think there's
a reason not to use it.
Abdo Haji-Ali
Programmer
In|Framez