मेरे कोड:निर्माता सदस्य चर
#include <iostream>
using namespace std;
class Foo
{
public:
int bar;
Foo()
{
bar = 1;
cout << "Foo() called" << endl;
}
Foo(int b)
{
bar = 0;
Foo();
bar += b;
cout << "Foo(int) called" << endl;
}
};
int main()
{
Foo foo(5);
cout << "foo.bar is " << foo.bar << endl;
}
उत्पादन:
Foo() called
Foo(int) called
foo.bar is 5
क्यों foo.bar
मूल्य 6 नहीं है? Foo()
कहा जाता है लेकिन bar
से 1 पर सेट नहीं करता है क्यों?
'फू() नहीं करना पड़ेगा,' एक बनाता है अज्ञात अस्थायी वस्तु और तुरंत इसे नष्ट कर देता है। यह क्या है कि आप _expected_ होने के लिए? – ildjarn