why cant we initialise a member variable inside a structure or a class?
structures
Collapse
X
-
You can, but it has to be inside of a constructor. Try the following:
[code=cpp]
#include <iostream>
using namespace std;
struct XYZZY
{
double foo, bar;
// Here's the constructor
XYZZY()
{
foo = 2.718281828;
bar = 1.618;
}
// Here's the destructor
~XYZZY(){};
} xyzzy;
int main (void)
{
cout<<"Foo = "<<xyzzy.foo<<e ndl
<<"Bar = "<<xyzzy.bar<<e ndl;
return;
}
[/code]
In this example, you've initialized the values of foo and bar inside the constructor within the struct XYZZY. If I understand your inquiry right, that should help. -
Originally posted by ankit4284why cant we initialise a member variable inside a structure or a class?
RaghuComment
Comment