Implicit class-Type conversions

Source: Internet
Author: User
Tags string format

In C + + primer, a constructor that can be invoked with a single argument defines an implicit conversion from the formal parameter type to the type.

Explain this:

For example, the parameter of a member function of object A of Class A should be an object of Class A. But the object B of a different type B is passed in, and the type of object B happens to be a single parameter constructor parameter type of a, then the system uses this B smart to create a temporary object C of Class A, Although both C and a are of type A, they are different objects.

This implicit conversion is very risky and can be added to a single parameter constructor to avoid this kind of stealth conversion with explicit.

I personally think that this is an artificial use error, and the compiler is precisely not pointed out.

It should have been A.A (a (b)), and what would be used as a a.a (b). Ha ha......

#include <string>

#include <iostream>

using namespace Std;

Class Fruit//define a category, named Fruit

{

String name;//defines a name member

String colour;//defines a colour member

Public

BOOL Issame (const fruit& otherfruit)//expecting parameter is another Fruit class object that tests for the same name

{

return name = = Otherfruit.name;

}

void print ()//defines a member of the Output name print ()

{

cout << colour << "" << name << Endl;

}

Fruit (const string& NST, const string& CST = "Green"): Name (NST), colour (CST) {}

Fruit () {}

};

int main ()

{

Fruit Apple ("Apple");

Fruit Orange ("orange");

cout << "Apple = orange?:" << apple.issame (orange) << Endl;

Implicit conversions

cout << "Apple =/apple/"? : "<< apple.issame (String (" Apple ")) << Endl;

return 0;

}

You will find that in the last use, we use a string type as an argument for a function that expects the fruit class parameter, and the result is true (1), don't be surprised, that's what I'm talking about, implicit class type conversions: " A constructor that can be invoked with a single argument defines an implicit conversion from the formal parameter type to that type. "(c + + Primer)

To start with a single argument, you can remove the default arguments for the constructor colour, which means that when you define an object must have two parameters, the file compilation cannot pass. Then the system knows how to change it, but it's more stringent here.

In the past when we constructed objects fruit apple ("apple") actually had a conversion from the C string format of const char * to String, where you Apple.issame ("Apple") The stupid system doesn't know how to help you convert it two times, so you have to use String () to cast first, then the system knows to help you convert from string to fruit, of course you can help him to do it. cout<< "Apple =/" apple/"?:" <<apple.issame (Fruit ("Apple"));  Fruit Apple = Fruit ("Apple"); Defines an fruit class object, Apple. That's how it's converted. But this is called explicit conversion, we do not mark out, the system to help us complete, called the implicit chant.

The point here is that if you show the transformation, you can do so regardless of the number of arguments, such as those mentioned above that require a constructor of two parameters.

int main ()

{

Fruit Apple ("Apple", "green");

Fruit Orange ("orange", "Yellow");

cout << "Apple = orange?:" << apple.issame (orange) << Endl;

Show conversions

cout << "Apple =/apple/"? : "<< apple.issame (Fruit (" Apple "," green ")) << Endl;

return 0;

}

What to do if you do not want to implicitly convert, in case of user misoperation.

C + + provides a way to suppress the implicit conversion of constructors, is to add the explicit keyword in front of the constructor, you will try to know that at that time you hope that the implicit conversion will cause the compilation to fail, but, to be explained, the explicit conversion can still proceed, in order not to provide the error source code example principle, The wrong situation is not available, try it yourself

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.