473,406 Members | 2,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

constructor calling a member function?

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
1 8458
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
by: Andy | last post by:
1) Is there any use of defining a class with a single constructor declared in private scope? I am not asking a about private copy constructors to always force pass/return by reference. 2) Is...
7
by: Karsten Hochkirch | last post by:
Hi, I have just encountered a problem when calling a virtual member function in a constructor. Only the memberfunction of the parent class is called instead of the overloaded function. I...
23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
4
by: Jerry Krinock | last post by:
I've written the following demo to help me understand a problem I'm having in a larger program. The "main" function constructs a Foo object, and then later "reconstructs" it by calling the...
14
by: gurry | last post by:
Suppose there's a class A. There's another class called B which looks like this: class B { private: A a; public : B() { a.~A() } }
7
by: Wu Shaohua | last post by:
Hi Guys, 1. As we know usually we should not define a constructor as inline. I also learned if we define a member function inside the class this member function will be automatically be...
3
by: Biswajit Jena | last post by:
Is it possible to call a member function in constructor of the same class ? how ? I have a templated member function which I want to call in the constructor of the same class. How will I do...
13
by: JD | last post by:
Hi, My associate has written a copy constructor for a class. Now I need to add an operator = to the class. Is there a way to do it without change her code (copy constructor) at all? Your help...
12
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.