473,698 Members | 2,602 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8536
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
3089
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 this in any way used to create singletons. Can someone say how? Cheers, Andy
7
1809
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 have attached a small example. Is that bug or feature. If feature: is there an easy way to get the
23
5174
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
7285
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 constructor again. In my larger program, I find that the member variables don't get re-initialized when "reconstructed". I don't have that problem in this demo, but the second time the constructor is called, its "this" points to a different location. ...
14
3204
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
16110
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 inline'ed. My question is: If I define a constructor (including its body) or another large member function inside the class, the constructor or the member function is inline or not? why? 2. I learned that if the member function is big we should not...
3
4198
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 it ?
13
3963
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 is much appreciated. JD
12
7202
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? class Trial { public: Trial() {
0
8680
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9169
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7738
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.