tags:

views:

139

answers:

2

let's say i have 2 classes

class B
{
   B() { /* BLA BLA */ };
   B(int a) { /* BLA BLA */ };
   B(int a,int b) { /* BLA BLA */ };
}

class A {
public :
  A(B  par);
}

i was wondering how can i call A's constructor with par having a deafult argument, as each of B constructors. (of course i would like see 3 examples, i don't expect all of them to exist together)

thanks

+8  A: 

You can do something like:

A(B par = B())
A(B par = B(1))
A(B par = B(1,2))

Full code as per comment:

class B
{
public:
   B() {  };
   B(int a) {};
   B(int a,int b) {};
};

class A {
public :
  A(B  par = B()/* or B(1) or B(1,2) */);

};
Naveen
does that work??
yesraaj
Yes. [15 char...]
Georg Fritzsche
curious about the reason for downvote..is something wrong in the answer?
Naveen
Did you mean to create `par` as object of `B` and pass the same to `A`, then it did not work for me, though I am not the down voter.
yesraaj
no.. they are the definition of A's constructor with default values for parameter B, I thought OP wanted to know how to specify the default parameters for a user defined type containing parametrized constructors..
Naveen
Do you mind post the full code?
yesraaj
@Naveen thanks, you just can have one of those constructor right?
yesraaj
Yes..that is what OP also mentioned in the post
Naveen
A(B par = B()) orA(B par = B(1)) or A(B par = B(1,2)) would have answered all my questions :).
yesraaj
A: 
A(B());//create a B object and pass it to A
A(B(1));
A(B(1,2));

or define 3 different constructor for A(but that does not sound good to me).

yesraaj