question about constructor



 DEVELOP > c-Plus-Plus > question about constructor

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
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()"
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER