Hi, I've created a class which has three constructors, two integer members & one const int member. So for one constructor I'm using initializer list to assign const int member but I am getting error in other two constructors
Here is my code:
class base
{
public:
base();
base(const int _count);
base(int a ,int b);
~base();
protected:
int m_ia , m_ib;
const int count;
};
base::base()
{
}
base::base(const int _count):count(_count)
{
}
base::base(int a , int b)
{
m_ia = a ;
m_ib = b;
}
base::~base()
{
}
void main()
{
base *obj2 = new base(1000);
getchar();
}
Number of Errors:2
1.'base::count' : must be initialized in constructor base/member initializer list at base()
2.'base::count' : must be initialized in constructor base/member initializer list at base(int a ,int b)
count
in the other 2 constructors in the same way... It doesn't automatically initialise it to 0 just because you didn't say anything. – flightvoid main
is not legal. The return type must beint
. Also, ifbase
is supposed to be a base class, that destructor should be virtual.obj2
should also be a normal object in this example. Right now, it's leaking memory. Finally, why not use a constructor initializer list for your other members as well? There's really almost never any down side. – chris