Sorry, I make a mistake here, I rewrite them.
It should be an composition, not a inheritance, sorry about that. But I
believe "*this" is not existed at that time. Is that right? Because
constructor B is not executed while a(*this) running.
Lorry Astra wrote:
Dear all,
I have a question in today's code review, here is my code example:
class A{
public:
A(B& b)
'B' hasn't been defined or declared at this point.
{....}
};
class B{
public:
B() : A(*this)
This is ill-formed unless 'B' derives from 'A'.
{....}
};
I think here exists a risk in class B constructor.
A risk of what?
> Because I think when
constructor A is executed before constructon B ("B() : A(*this)"),
constructor A uses an object of class B as parameter.
So?
> But I believe the
object of class B is not generated while constructor A running.
You mean it hasn't been completely constructed yet, yes?
> So I think
"A(*this)" is a risk.
A risk of *what*?
So I think if this code is like that, the executed sequence will be:
1. A(*this) -> A(B& b)
2. B()
Is that right? Are there any errors in my description?
Yes, plenty. First off, the code is ill-formed. Please fix the code
and ask your question again then. In the mean time read the C++ FAQ,
section 10, question 10.7.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask