constructor calling a member function?

N

newbie

This is a snippet from C++ FAQs, which I have never done--- when I do
such a thing, I would declare function used by constructor ( in this
example, init() ) as static. But I do understand that it would be
great if the following practice is valid.

But by OO concept, it seems not to very good because init() belongs to
an object, which hasn't be constructed.
Can you give any comment on my question?


class Foo {
public:
Foo(char x);
Foo(char x, int y);
...
private:
void init(char x, int y);
};

Foo::Foo(char x)
{
init(x, int(x) + 7);
...
}

Foo::Foo(char x, int y)
{
init(x, y);
...
}

void Foo::init(char x, int y)
{
...
}
 
A

Andre Kostur

This is a snippet from C++ FAQs, which I have never done--- when I do
such a thing, I would declare function used by constructor ( in this
example, init() ) as static. But I do understand that it would be
great if the following practice is valid.

Static won't do you any good. init() wouldn't be able to touch class
members (short of passing a reference to the object, but then you're back
where you started...).
But by OO concept, it seems not to very good because init() belongs to
an object, which hasn't be constructed.
Can you give any comment on my question?

Well, by the time init() is called, all of the constituent members have
already been initialized. You'll get into bigger trouble if you called
member functions within the initializer list, or pass "this" to other
functions since "this" hasn't even finished initializing yet (the other
functions have the potential to attempt to use the member variables that
haven't been initialized yet). What you won't get is "proper" virtual
function behaviour (maybe "potentially unexpected behaviour" is a better
term). Virtual calls won't invoke further derived members. While this
class' constructor is executing, it isn't the derived class (yet).
 
A

Amal P

This is a snippet from C++ FAQs, which I have never done--- when I do
such a thing, I would declare function used by constructor ( in this
example, init() ) as static. But I do understand that it would be
great if the following practice is valid.

There is no need to use static in this case. And static will make
it hard to call any member function from it and you wont be able to
access any non static members. So static dont serve this purpose.
But by OO concept, it seems not to very good because init() belongs to
an object, which hasn't be constructed.

It is of course safe to call any non virtual member function from
constructor because object is completely setup before first line of
user code is executed. The virtual table is also properly initialized
before executing first line of user code. So obliviously there is
nothing wrong in calling a member function from constructor and
thereby the below usage is exactly valid.
Can you give any comment on my question?

class Foo {
public:
Foo(char x);
Foo(char x, int y);
...
private:
void init(char x, int y);
};

Foo::Foo(char x)
{
init(x, int(x) + 7);
...
}

Foo::Foo(char x, int y)
{
init(x, y);
...
}

void Foo::init(char x, int y)
{
...
}

In the above code the Foo::init(x,y) will be correctly called.
Because before the execution of first line in constructor the object
is fully setup.

Even the call of the non pure virtual function will not do any
harm. The only thing is while calling a virtual function from
constructor or destructor be sure that the virtual function mechanism
will not work in constructor/destructor and it will cause the calling
of virtual function implementation of same class. The call doesnt work
because in first case the class derived class is not yet constructed
and the derived class is already destructed in the later case.

Thanks and regards,
Amal P.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,076
Latest member
OrderKetoBeez

Latest Threads

Top