How can I overload char*? [duplicate]

2019-09-20 04:46发布

问题:

This question already has an answer here:

  • How to pass optional arguments to a method in C++? 7 answers

I was asked that in a method class receive a char* and copy that in the member of the class, and if I don´t receive any parameter assign "Sin nombre" to the parameter

void VEHICULO::Set_Nombre(const char* c){
      //No problem here
}

int main(){
      VEHICULO a;
      a.Set_Nombre("FERRARI");//ITS OK
      a.Set_Nombre();         //the class doesnt know that method
}

know how to copy the passed char* or copy "Sin nombre", but when in main I call without a parameter the class doesn´t recognize it, and it should work with the same method, I can´t code the method Set_Nombre(void)

回答1:

C++ default arguments.

void VEHICULO::Set_Nombre(const char* c = "Sin Nombre")
{
}


标签: c++ class