Click here to Skip to main content
15,907,001 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi. oh i am sorry . i have question again.
class A
    int data;
    A(int d):data(d){}
class B

    A s;
    B(A e):s(e){}
int _tmain(int argc, _TCHAR* argv[])
    A ins(12);
    B ins1(ins);
    return 0;

in above code i make an object of class A in class B. when an object want to make . the constructor should call. in the line
A s;

the constructor of class A should call . yes? but there isn't any default constructor to call. i think this line is false and i should write
A s(0);

because the constructor of class A have an argument . but this is false and the
A s;

is true. but i don't know why ?
please help me

1 solution

No, the "A s;" is part of your "B" class declaration - the actual "s" member is not constructed until an instance of "B" is created.

The "B(A e):s(e){}" line is a constructor for "B" and the ":s(e)" part of it is the initialisation list where the "A(int)" constructor is called.
Share this answer
sara74 21-Jun-14 8:10am    
thanks very much my friend

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900