Assume a class declared like this (I have reduce it to 1 float for berevity)
class MyClass
{
public:
MyClass(float initx);
setX(float newx);
private:
float x;
}
You are say since the constructor is implemented as
MyClass::MyClass(float initx)
{
x = initx;
}
you could just call setX which does the same thing. The mistake is that you are assign in the constructor when it is more preferable to use initialiser lists so you constructor should really be
MyClass::MyClass(float initx)
: x(initx)
{
}
Nothing like setX!
A float is a simple case but get into the habit of using intiialiser lists. You will often want to use them if you have a class hierarchy anyway to initialise base classes in a specific way.
if the constructor calls some code that is also called by a class method then probably the best thing to do is to put the code into a private method that both the constructor and public member call. That is separate the implementation from the interface. Also also allows for such things as differing checks on data validity before calling the common code.