C++ implicit type conversion example sharing

  • 2020-04-02 02:16:25
  • OfStack



#include <iostream>
#include <string>
#include <vector>
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::vector;
class People {
    public:
        People() = default;
        People(string s):name(s) { }
        string getName() const { return name; }
        static vector<string> &getVector() { return name_arr; }
        //Implicit type conversion, which generates a temporary quantity with string, so it can be bound to a const parameter
        static void addToVector(const People &p) { 
            name_arr.push_back(p.getName());
        }
    private:
        string name = "";
        static vector<string> name_arr;
};
vector<string> People::name_arr = {};
int main(int argc, const char *argv[])
{
    People p;
    cout << "p :" << endl;
    cout << p.getName() << endl;
    People tom("tom");
    People::addToVector(tom);
    string Bob = "Bob";
    People::addToVector(Bob);//Implicit type conversion
    //People::addToVector("Bob");// One step only Implicit type conversion
    vector<string> v = People::getVector();
    cout << "name_arr:" << endl;
    for (const auto &p : v) {
        cout << p << " ";
    }
    cout << endl;
    string myName = "guo";
    People guo = myName; //Implicit type conversion Allows to copy the transformation of the initialization form 
    cout << guo.getName() << endl;
    return 0;
}


Let's do another example


#include <string>
#include <iostream>
using namespace std;
class Fruit               //Define a class called Fruit
{
 string name;     //Define a name member & NBSP; & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent
 string colour;   //Define a colour member

public:
 bool isSame(const Fruit &otherFruit)   //The expected parameter is another Fruit class object to test for the same name
 {
  return name == otherFruit.name;
 }
 void print()              //Define a member print() with an output name
 {
  cout<<colour<<" "<<name<<endl;
 }
 Fruit(const string &nst,const string &cst = "green"):name(nst),colour(cst){}  //The constructor

 Fruit(){}
};
int main()
{
 Fruit apple("apple");
 Fruit orange("orange");
 cout<<"apple = orange ?: "<<apple.isSame(orange)<<endl;  //No problem, it must be different
 cout<<"apple = /"apple/" ?:"<<apple.isSame(string("apple")); //Using a string as a parameter?

    return 0;
}

You will find that the use of the last, we use a string type as a forward to Fruit type and the parameters of the function, the result was concluded that it is true (1), don't be surprised, this is I want to say something now, implicit type conversion: "you can use a single argument to invoke a constructor defined from the parameter type to the type of an implicit conversion." (C++ Primer) first want a single argument, you can constructor colour default argument removed, that is, the definition of an object must have two arguments, file compilation can not pass. And then when this condition is met, the system knows what to do, but it's a little bit more rigorous: ), we construct the object before Fruit (apple "apple") also has a conversion, actually from the const char * C string format, converted to a string, here, you again apple. IsSame (" apple "), stupid system does not know to help you convert twice, so you have to use the string () casting first, and then system will know to help you from string implicitly converted to Fruit,, of course, in fact you can also be done to help him. cout < < Apple apple = "/"/"? :" < < Apple. IsSame (Fruit (" apple ")); Like this. Reference example 1.2: Fruit apple = Fruit("apple");   // define a Fruit class object apple. So that's how you convert. But this is called an explicit transformation, we don't label it, the system does it for us, it's called an implicit shell. The point here is that if you show the transformation it doesn't matter how many arguments there are, such as in the case of the constructor that must have two arguments mentioned earlier.

Ex. :


#include <string>
#include <iostream>
using namespace std;
class Fruit               //Define a class called Fruit
{
 string name;     //Define a name member & NBSP; & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent & have spent
 string colour;   //Define a colour member

public:
 bool isSame(const Fruit &otherFruit)   //The expected parameter is another Fruit class object to test for the same name
 {
  return name == otherFruit.name;
 }
 void print()              //Define a member print() with an output name
 {
  cout<<colour<<" "<<name<<endl;
 }
 Fruit(const string &nst,const string &cst):name(nst),colour(cst){}  //The constructor

 Fruit(){}
};
int main()
{
 Fruit apple("apple","green");
 Fruit orange("orange","yellow");
 cout<<"apple = orange ?: "<<apple.isSame(orange)<<endl;  //No problem, it must be different
 cout<<"apple = /"apple/" ?:"<<apple.isSame(Fruit("apple","green")); //Explicit conversion
    return 0;
}

What if you don't want to implicitly convert in case the user does something wrong? C++ provides a way to suppress implicit conversions of constructors by adding the explicit keyword before the constructor. If you try, you'll know that implicit conversions will fail to compile if you want them to, but it's important to note that explicit conversions can still occur.


Related articles: