By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,906 Members | 1,388 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,906 IT Pros & Developers. It's quick & easy.

Pointer to a Partially-Constructed Object

P: n/a
Hello all,

Please consider the following code:

class Bar;

class Foo {
Bar *m_pBar;
public:
Foo(Bar *pBar): m_pBar(pBar) {}
};

class Bar {
Foo m_Foo;
public:
Bar(): m_Foo(this) {}
};

It would certainly be wrong for Foo::Foo() to dereference pBar since
the Bar object is still being constructed and therefore does not
exist. However, is it wrong to even cache off the pointer for later
use as shown?

Thanks,
Dave

May 28 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
be***********@yahoo.com wrote:
Hello all,

Please consider the following code:

class Bar;

class Foo {
Bar *m_pBar;
public:
Foo(Bar *pBar): m_pBar(pBar) {}
};

class Bar {
Foo m_Foo;
public:
Bar(): m_Foo(this) {}
};

It would certainly be wrong for Foo::Foo() to dereference pBar since
the Bar object is still being constructed and therefore does not
exist. However, is it wrong to even cache off the pointer for later
use as shown?
No. If you only store the pointer value, that's fine.

May 28 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.