Original Post
So I was wondering when writing a piece of code this morning, how do other people decide what to put in a constructor and what to put in another Initialisation method. For example, if in a class you had the following.. There are loads of different ways of doing these things, for instance passing an int when creating the class and setting it from that but would anyone even do the following... How do you personally deal with this? Or do you just decide on a way you prefer and do it like that consistently.. Personally I normally put a lot of it into the constructor, but im guessing there's a reason not to do this? Cheers, J
class foo
{
private:
int m_MyInt;
SomeClass* pSomeClass;
public:
foo()
{
// If I wanted m_MyInt to have a certain value when I
// first use my class should I set it here, or use a
// method to set it which I call from here.
// Method 1:
m_MyInt = 2;
// Method 2:
Init();
// The same can be said for allocating memory for the
// pointer to SomeClass.. should it be done here or
// in the Init() method...
}
void Init()
{
m_MyInt = 2;
}
}
foo(int inInt)
{
m_MyInt = inInt;
}
foo(int inInt)
{
Init(inInt);
}