简体   繁体   中英

constructor as default argument

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

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) */);

};
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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM