Variable in a class

hello All,
I just tried with the following code & got some error in both Turbo & Visual studio

class ABC{
int a=10;
ABC(){}
}

1) why cant we declare & initialize a variable like this (a=10)
2) Is there any way to initialize variables
3) It is possible to initialize variables like this in C# etc


plz help!!!
Thanks in Advance
You can do it if it's a static const but otherwise you have to do it in the constructor
1
2
3
4
5
6
class ABC{
int a;
ABC()
:a(10)
{}
}

Last edited on
ABC():a()
{
}

what does this mean??? Initialization list is there any use of declaring like this. .
closed account (z05DSL3A)

This link may help shed some light on it:
http://msdn.microsoft.com/en-us/magazine/cc301399.aspx
In C++ you can supply a member initialisation list in a constructor. It has a leading semi-colon and is comma separated and comes before the first opening brace of the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
class ABC
{
   ABC(void)
   int a, b, c;
   std::string str;
}

ABC::ABC(void)
:a(0), b(0), c(12), str("Hello")
{
   // code body.  Variables have been initialised to:
  // a = 0, b = 0, c = 12 and str = "Hello"
}

Ok i got it
thanks for every bodys help
Topic archived. No new replies allowed.