473,400 Members | 2,163 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,400 software developers and data experts.

deferring member object instantiation to constructor

Is there any way to defer instantiation of " t1" to the constructor of
"test1"? because, "t1" can only meaningfully initialized in the "test1"
constructor.

#cat test.cpp
#include <iostream>
using namespace std;

class test
{
public:
test()
{
cout << "hello" << endl;
}
};

class test1
{
test t;

public:
test1()
{
cout << "hello1" << endl;
}
};

int main(int argc, char *argv[])
{
test1 t1;

return 0;
}
#g++ test.cpp
#./a.out
hello
hello1
#
thanks in advance.

May 30 '06 #1
8 2220
v4vijayakumar wrote:
Is there any way to defer instantiation of " t1" to the constructor of
"test1"?
What does that mean? 't1' is a local variable of the 'main' function.
It's instantiated when the control passes its declaration/definition.
because, "t1" can only meaningfully initialized in the
"test1" constructor.
Huh? "Meaningfully initialized"? As opposed to what? Meaninglessly
initialized?
#cat test.cpp
#include <iostream>
using namespace std;

class test
{
public:
test()
{
cout << "hello" << endl;
}
};

class test1
{
test t;

public:
test1()
{
cout << "hello1" << endl;
}
};

int main(int argc, char *argv[])
{
test1 t1;

return 0;
}
#g++ test.cpp
#./a.out
hello
hello1
#


OK, that's actual output. What's the desired output?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 30 '06 #2

v4vijayakumar wrote:
Is there any way to defer instantiation of " t1" to the constructor of
"test1"? because, "t1" can only meaningfully initialized in the "test1"
constructor. .... test1 t1;


I think you're confusing identifiers. t1 is an object of type test1,
and therefore
t1 is initialized by the (default) constructor test1::test1( )

HTH,
Michiel Salters

May 30 '06 #3
By "t1" I assume you actually mean t.
I'm not sure if this helps you if you have some very special case, but normally
using an initialization list would solve the problem of not
default-constructing t. The only other way around as I see it would be to have
a pointer to t i.e a test* t; and allocate it with new as the necessary
information is available (don't forget to delete it in the destructor of
test1). Without more context it is difficult to say something more intelligent.

/Daniel Aarno

v4vijayakumar skrev:
Is there any way to defer instantiation of " t1" to the constructor of
"test1"? because, "t1" can only meaningfully initialized in the "test1"
constructor.

#cat test.cpp
#include <iostream>
using namespace std;

class test
{
public:
test()
{
cout << "hello" << endl;
}
};

class test1
{
test t;

public:
test1()
{
cout << "hello1" << endl;
}
};

int main(int argc, char *argv[])
{
test1 t1;

return 0;
}
#g++ test.cpp
#./a.out
hello
hello1
#
thanks in advance.

May 30 '06 #4

Michiel.Salt...@tomtom.com wrote:
v4vijayakumar wrote:
Is there any way to defer instantiation of " t1" to the constructor of
"test1"? because, "t1" can only meaningfully initialized in the "test1"
constructor.

...
test1 t1;


I think you're confusing identifiers. t1 is an object of type test1,
and therefore
t1 is initialized by the (default) constructor test1::test1( )

HTH,
Michiel Salters


Suppose you have "ifstream" member object. This member object can only
be meaningfully initialized through the constructor parameter. How this
could be done?

May 30 '06 #5
On Tue, 30 May 2006 05:41:06 -0700, v4vijayakumar wrote:
Is there any way to defer instantiation of " t1" to the constructor of
"test1"? because, "t1" can only meaningfully initialized in the "test1"
constructor.
Huh? That sentence doesn't seem to make any sense. t1 is of the type
test1, and already is initialized in the "test1" constructor...

#cat test.cpp
#include <iostream>
using namespace std;

class test
{
public:
test()
{
cout << "hello" << endl;
}
};

class test1
{
test t;

public:
test1()
{
cout << "hello1" << endl;
}
};

int main(int argc, char *argv[])
{
test1 t1;

return 0;
}
#g++ test.cpp
#./a.out
hello
hello1
#


What is this supposed to be showing us? I guess you're showing us the
current behaviour, but you haven't described what your desired behaviour
is.
May 30 '06 #6
v4vijayakumar wrote:
Is there any way to defer instantiation of " t1" to the constructor of
"test1"? because, "t1" can only meaningfully initialized in the "test1"
constructor.

#cat test.cpp
#include <iostream>
using namespace std;

class test
{
public:
test()
{
cout << "hello" << endl;
}
};

class test1
{
test t;

public:
test1()
{
cout << "hello1" << endl;
}
};

int main(int argc, char *argv[])
{
test1 t1;

return 0;
}
#g++ test.cpp
#./a.out
hello
hello1


Since your question as stated doesn't make sense, I think you meant to
ask: Can I defer the calling of the constructor test::test() for the
member object t within test1's constructor? The answer is no and yes --
no, because the member object will be default constructed in the
(implicit) initializer list before the body of the constructor
executes, and yes, because you could change the object to a (smart)
pointer so that construction of t is delayed until you are ready to do
it. For instance:

class test1
{
public:
test1()
{
cout << "hello1" << endl;

// Do some other stuff to get ready for making a test object

t.reset( new test );
}

private:
std::auto_ptr<test> t;

// Disable functions because of auto_ptr's
// destructive copy semantics
test1( const test1& );
test1& operator=( const test1& );
};

Of course, using std::tr1::scoped_ptr (aka boost::scoped_ptr) would be
preferable since it would minimize mistakes with copying auto_ptrs.

Keeping the rest of the program the same but substituting my test1
class would give this output:

hello1
hello

Cheers! --M

May 30 '06 #7
v4vijayakumar wrote:
Michiel.Salt...@tomtom.com wrote:
v4vijayakumar wrote:
Is there any way to defer instantiation of " t1" to the constructor of
"test1"? because, "t1" can only meaningfully initialized in the "test1"
constructor.

...
test1 t1;


I think you're confusing identifiers. t1 is an object of type test1,
and therefore
t1 is initialized by the (default) constructor test1::test1( )

HTH,
Michiel Salters


Suppose you have "ifstream" member object. This member object can only
be meaningfully initialized through the constructor parameter. How this
could be done?


sorry, my mistake. I haven't included "fstream".

May 30 '06 #8
v4vijayakumar wrote:
Michiel.Salt...@tomtom.com wrote:
v4vijayakumar wrote:
Is there any way to defer instantiation of " t1" to the constructor of
"test1"? because, "t1" can only meaningfully initialized in the "test1"
constructor.

...
test1 t1;


I think you're confusing identifiers. t1 is an object of type test1,
and therefore
t1 is initialized by the (default) constructor test1::test1( )

HTH,
Michiel Salters


Suppose you have "ifstream" member object. This member object can only
be meaningfully initialized through the constructor parameter. How this
could be done?


Use an initializer list
(http://www.parashift.com/c++-faq-lit....html#faq-10.6) or a
pointer to the member object as described in my other post.

Cheers! --M

May 30 '06 #9

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

Similar topics

28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
7
by: Dave L | last post by:
I am inquiring about the feasability of being able to determine when an object of a certain type is instantiated. I have a base type that I want to be able to determine when each instance is...
22
by: Ruben Van Havermaet | last post by:
Hi, I have a problem using member functions in derived classes that override virtual member functions of base classes. The following pieces of (simplified) code don't work. Can anybody give...
6
by: Marty McDonald | last post by:
public class BaseDALC { //Don't allow instantiation private BaseDALC() // To compile properly, I had to change this to "protected" { } } .... public class StatisticsDALC : BaseDALC {
7
by: John A Grandy | last post by:
I'm trying to get a decent idea of the relative performance of three types of implementations of data-access classes in ASP.NET 2.0. I believe this boils down to a more basic question regarding...
4
by: Søren M. Olesen | last post by:
Hi Is it (any way) possible using reflection to set an indicator (property, variable, attribute,..). on an object before it actually instantiated, so that this indicator can be used in the...
1
by: ITMozart | last post by:
I wrote this piece of code: class A { public A() { someMeth(); } public void someMeth() { out.println(entries); }
17
by: Juha Nieminen | last post by:
As we know, the keyword "inline" is a bit misleading because its meaning has changed in practice. In most modern compilers it has completely lost its meaning of "a hint for the compiler to inline...
5
by: chgans | last post by:
Hi all, I'm having difficulties with some template static member, especially when this member is a template instance, for example: ---- template<typename T> class BaseT { public: static...
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
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,...
0
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...

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.