deferring member object instantiation to constructor

V

v4vijayakumar

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.
 
A

Andre Kostur

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.
 
V

Victor Bazarov

v4vijayakumar said:
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
 
M

Michiel.Salters

v4vijayakumar said:
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
 
D

Daniel Aarno

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:
 
V

v4vijayakumar

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?
 
M

mlimber

v4vijayakumar said:
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
 
V

v4vijayakumar

v4vijayakumar said:
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".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top