वह नियम कक्षा के सदस्यों पर लागू नहीं होता है। यह सी ++ 03 मानक के 12.2.5 में कहा गया है:
A temporary bound to a reference member in a constructor's ctor-initializer
persists until the constructor exits.
तुलना में अस्थायी पिछले लंबे समय तक बना अर्थ होगा उस वर्ग के हिस्से के रूप में रखा जाना चाहिए, ताकि उसके जीवन हो सकता है के लिए होता है अस्थायी बनाए रखा। यह असंभव होगा अगर कन्स्ट्रक्टर एक अलग संकलन इकाई में था, क्योंकि कक्षा को परिभाषित किया जाना चाहिए जब कक्षा को परिभाषित किया जाना चाहिए।
// header file
struct A {
A();
B &b;
};
// file1.cpp
void f()
{
A a; // Reserve the size of a single reference on the stack.
}
// file2.cpp
A::A()
: b(B()) // b is referencing a temporary, but where do we keep it?
// can't keep the temporary on the stack because the
// constructor will return and pop everything off the stack.
// Can't keep it in the class because we've already said the
// class just contains a single reference.
{
}
स्रोत
2012-07-07 16:11:05
स्पॉट ऑन, मुझे नहीं पता कि इसके लिए कोई अपवर्तक क्यों नहीं थे। आज धीमा दिन :( –
संक्षेप और व्यापक। बहुत बहुत धन्यवाद! – davka