Class reference member problem

T

tech

Hi, i have the following situation

class B
{

static B createBobj();
}

Class A
{
func()
{
m_obj = createobj();
}

B& m_obj;
};

Obviously doesn't compile so

In class A i want to hold a reference to an instance of class B
created by the
factory method createBObj. i can't initialise my reference member in
constructor as
class A needs to do some other initialisation before creating instance
of B.

So how to solve this problem?
 
M

Martin Vuille

ps.com:
In class A i want to hold a reference to an instance of class
B created by the
factory method createBObj. i can't initialise my reference
member in constructor as
class A needs to do some other initialisation before creating
instance of B.

So how to solve this problem?

Have class A hold a pointer to the instance of B instead?

MV
 
A

anon

tech said:
Hi, i have the following situation

class B
{

static B createBobj();

I hope it is not private
}

Class A
{
func()
{
m_obj = createobj();

Here you point your reference to a temporary object, which is destroyed
when func() ends.
By the way, where is createobj() declared in class A?
}

B& m_obj;
};

Obviously doesn't compile so

Yes, for many reasons
In class A i want to hold a reference to an instance of class B
created by the
factory method createBObj. i can't initialise my reference member in
constructor as
class A needs to do some other initialisation before creating instance
of B.

So how to solve this problem?

Can you change m_obj to be of type B or B*?
 
T

tech

ok should be

func()
{
m_obj = B:createobj();

}

ok if i change m_obj to pointer, createobj returns object
so it will go out of scope anyway, so how do i solve this?
Where do i create the instance of B so it doesn't go out of scope
given that creatobj returns obj not on heap.
 
A

anon

tech said:
> ok should be
>
> func()
> {
> m_obj = B:createobj();

m_obj = B::createobj();
>
> }
>
> ok if i change m_obj to pointer, createobj returns object
> so it will go out of scope anyway, so how do i solve this?

In the createobj() function create an object of type B, and return a pointer
for example
B* createobj()
{
B *p = new B;
return p;
}
then do not forget to delete created object
> Where do i create the instance of B so it doesn't go out of scope
> given that creatobj returns obj not on heap.

The way you presented, I think it is not possible because createobj()
returns a temporary object of type B.
If you make m_obj of type B, then you can do what you did, but you are
invoking the B's copy-constructor
 
D

Daniel T.

tech said:
Hi, i have the following situation

class B
{

public: // I'm assuming either createBobj is public, or
// A is a friend of B.
static B createBobj();
}
// I'm assuming you meant to put a ';' after that closing brace.

// I'm assuming you meant "class A"

// I'm assuming func() returns void
{
m_obj = createobj();

// I'm assuming that was supposed to be "m_obj = B::createBobj();"
}

B& m_obj;
};

Obviously doesn't compile so

Obviously. :)
In class A i want to hold a reference to an instance of class B
created by the
factory method createBObj. i can't initialise my reference member in
constructor as
class A needs to do some other initialisation before creating instance
of B.

So how to solve this problem?

Lastly, I'm assuming you can't change class B, and that B has proper
copy construction semantics...

class A {
B m_obj;
void func() {
m_obj = B::createBobj();
}
};

It will not hold the exact instance created by createBobj(), but it will
hold an instance that is in the same state.

Or did I make too many assumptions... :)
 
J

James Kanze

ok should be
func()
{
m_obj = B:createobj();
}
ok if i change m_obj to pointer, createobj returns object
so it will go out of scope anyway, so how do i solve this?

You need to make a copy.
Where do i create the instance of B so it doesn't go out of
scope given that creatobj returns obj not on heap.

Either in the class, or on the heap. If m_obj has type B, the
above will copy assign the temporary to it. If m_obj has type
B*, then:

m_obj = new B( B::creatobj() ) ;

will copy construct an object on the heap, and assign its
address to m_obj.
 

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

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,201
Latest member
KourtneyBe

Latest Threads

Top