| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"July" |
| Date: |
17 Jul 2005 08:22:43 AM |
| Object: |
question about constructor |
consider the following code:
#include <iostream>
using namespace std;
class A {
public:
A() {
cout << 'A';
}
};
class B {
public:
B() {
cout << 'B';
}
private:
A a;
};
int main()
{
B b;
}
Why the output is AB not BA ?
And when the A::A() be called ?
.
|
|
| User: "John Carson" |
|
| Title: Re: question about constructor |
17 Jul 2005 08:34:10 AM |
|
|
"July" <julyfirst789@yahoo.com> wrote in message
news:1121606563.597567.93710@o13g2000cwo.googlegroups.com
consider the following code:
#include <iostream>
using namespace std;
class A {
public:
A() {
cout << 'A';
}
};
class B {
public:
B() {
cout << 'B';
}
private:
A a;
};
int main()
{
B b;
}
Why the output is AB not BA ?
And when the A::A() be called ?
The order of construction is:
1. base object constructor.
2. member object constructor.
3. body of own constructor function.
--
John Carson
.
|
|
|
|
| User: "Peter Julian" |
|
| Title: Re: question about constructor |
17 Jul 2005 11:04:27 AM |
|
|
"July" <julyfirst789@yahoo.com> wrote in message
news:1121606563.597567.93710@o13g2000cwo.googlegroups.com...
consider the following code:
#include <iostream>
using namespace std;
class A {
public:
A() {
cout << 'A';
}
};
class B {
public:
B() {
cout << 'B';
}
private:
A a;
};
int main()
{
B b;
}
Why the output is AB not BA ?
And when the A::A() be called ?
Because you need to build the engine *before* you can start building the
car. The same goes with any other component in such a "composition".
.
|
|
|
|
| User: "Xie Yubo" |
|
| Title: Re: question about constructor |
17 Jul 2005 08:29:45 AM |
|
|
July wrote:
consider the following code:
#include <iostream>
using namespace std;
class A {
public:
A() {
cout << 'A';
}
};
class B {
public:
B() {
cout << 'B';
}
private:
A a;
};
int main()
{
B b;
}
Why the output is AB not BA ?
And when the A::A() be called ?
Obviously, the member object of the class is first constructed.
--
Best Regards
Xie Yubo
Email: Website: http://xieyubo.cn/
Harbin Institute of Technology
Phone: 86-451-86416614 Fax: 86-451-86413309
.
|
|
|
|
| User: "Ken Wilson" |
|
| Title: Re: question about constructor |
17 Jul 2005 09:05:59 AM |
|
|
On 17 Jul 2005 06:22:43 -0700, "July" <julyfirst789@yahoo.com> did
courageously avow:
consider the following code:
#include <iostream>
using namespace std;
class A {
public:
A() {
cout << 'A';
}
};
class B {
public:
B() {
cout << 'B';
}
private:
A a;
};
int main()
{
B b;
}
Why the output is AB not BA ?
And when the A::A() be called ?
Because the base class is called first, and then any derived classes,
in order?
Ken Wilson
"Coding, coding, over the bounding main()"
.
|
|
|
|

|
Related Articles |
|
|