By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,387 Members | 1,729 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,387 IT Pros & Developers. It's quick & easy.

constructor calling a member function?

P: n/a
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)
{
...
}

Jul 5 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Jul 6, 8:15 am, newbie <mitbb...@yahoo.comwrote:
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.

Jul 6 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.