470,833 Members | 1,337 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,833 developers. It's quick & easy.

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 2114
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

28 posts views Thread by Daniel | last post: by
22 posts views Thread by Ruben Van Havermaet | last post: by
6 posts views Thread by Marty McDonald | last post: by
7 posts views Thread by John A Grandy | last post: by
1 post views Thread by ITMozart | last post: by
17 posts views Thread by Juha Nieminen | last post: by
5 posts views Thread by chgans | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.