473,322 Members | 1,405 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,322 software developers and data experts.

Building constructors with shared functionality

I want to call a constructor from another constructor without constructing
an object. In other words, I want to partially initialize the object I'm
working with with one constructor, and finish the job with another.

class Foo
{
public:
Foo();
Foo(int i);
private:
int a, b;
};

Foo::Foo()
{
a = b = 0;
}

Foo::Foo(int i)
{
Foo(); //or should it be Foo::Foo()?
a = i;
}

Is there any way to do this in C++ that does not involve private helper
methods?
Jul 22 '05 #1
4 1040
On Mon, 12 Jul 2004 03:05:14 -0400, Aguilar, James
<jf**@cec.NOBOTSwustl.edu> wrote:
I want to call a constructor from another constructor without
constructing
an object. In other words, I want to partially initialize the object I'm
working with with one constructor, and finish the job with another.

class Foo
{
public:
Foo();
Foo(int i);
private:
int a, b;
};

Foo::Foo()
{
a = b = 0;
}

Foo::Foo(int i)
{
Foo(); //or should it be Foo::Foo()?
a = i;
}

Is there any way to do this in C++ that does not involve private helper
methods?


No. But if you really need a partially initialised object that might be a
clue that what you really need is to split your object into a base class
and a derived class. While the derived class is being created the base
class will be created first and that is your 'partially initialised
object'.

john
Jul 22 '05 #2

"Aguilar, James" <jf**@cec.NOBOTSwustl.edu> wrote in message
news:cc**********@newsreader.wustl.edu...
I want to call a constructor from another constructor without constructing
an object. In other words, I want to partially initialize the object I'm
working with with one constructor, and finish the job with another.

class Foo
{
public:
Foo();
Foo(int i);
private:
int a, b;
};

Foo::Foo()
{
a = b = 0;
}

Foo::Foo(int i)
{
Foo(); //or should it be Foo::Foo()?
a = i;
}


http://www.parashift.com/c++-faq-lit....html#faq-10.3
You can't call a constructor. Foo() in your above definition of Foo(int i)
is a temporary object, it doesn't call the constructor. Instead write some
private member function init and call it.
-Sharad

Jul 22 '05 #3
* Sharad Kala:

http://www.parashift.com/c++-faq-lit....html#faq-10.3
You can't call a constructor
.... on the 'this' object (without using placement new, which you
shouldn't anyway).

Read the FAQ and HS carefully.

. Foo() in your above definition of Foo(int i)
is a temporary object, it doesn't call the constructor. Instead write some
private member function init and call it.


Right.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #4

"Alf P. Steinbach" <al***@start.no> wrote in message
news:40***************@news.individual.net...
* Sharad Kala:

http://www.parashift.com/c++-faq-lit....html#faq-10.3
You can't call a constructor
... on the 'this' object (without using placement new, which you
shouldn't anyway).


Read the FAQ and HS carefully.


Well, I have :-) I shouldn't have been in a haste though.
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
33
by: Joe Fallon | last post by:
1. I have a Base class that has a certain amount of functionality. 2. Then I have a CodeSmith generated class that inherits from the Base class and adds functionality. 3. Since I want to be able...
14
by: Paul Bromley | last post by:
Forgive my ignorance on this one as I am trying to use a Singleton class. I need to use this to have one instance of my Class running and I think I understand how to do this. My question however is...
4
by: Sathyaish | last post by:
What is a private constructor, and why would a class have one? What are the other kinds of constructors besides: (1) public constructors; and (2) parameterized constructors And I understand...
10
by: John | last post by:
Trying to find out what is essential / optional, I made an extremely simple Class and Module combination to add two numbers. (see below) It appears that an empty constructor is needed n order to...
3
by: John | last post by:
Before anything else, thanks Marina, Workgroups and Ralf, for your help so far. I am now able to better define the question! After adding more console printout lines to CSum, I tried all...
2
by: David Garamond | last post by:
Does it currently work? src/backend/utils/mb/conversion_procs/*/ is not building anything, and 'make install' fails because it tries to copy *.so files. -- dave ...
0
by: e r m | last post by:
I'm having the exact same problem as reported in Bug ID #'s 33476, 36305, 36020, and 34516. PHP 5.1.1 and 5.1.2 won't link against libmysqlclient.a. Environment details: Sun V440 Solaris 9...
5
by: Xenomech | last post by:
Is there a way to explicitly call a shared constructor or shared finalizer in a static class?
1
by: timexsinclair2068 | last post by:
Hi, I'm getting some exceptions when creating new threads. That might -or might not- have to do with a probable (?) re-execution of shared constructors. Is that possible? Do shared constructors...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.