Implement the gasket class-bottom

Source: Internet
Author: User

Time flies so fast. Remember to write and implement the gasket class. When I encountered a problem, I wanted to complete it after a week. How can I see it in a twinkling of an eye? Two months have passed... I am really lazy !! Today, I want to finish writing the gasket class.
The function object solves the implementation problem of the gasket class, but has to introduce a disgusting Macro. Is there a way to avoid the macro?
To avoid macros, _ UNCC must be the class name. _ UNCC (szedittext) simply constructs a temporary class instance, and only the constructor is called. What? Let the constructor directly return a wchar_t * type? How is this possible .....
But imagine how the system would do it if we just did it here? Obviously, the compiler tries to implicitly convert the temporarily created _ UNCC Class Object to the wchar_t * type, but the conversion will certainly fail, because the system does not know how to convert the two types, why can't I manually add type conversion to the _ UNCC class to solve the problem?
The C ++ class supports type conversion. It treats type conversion as a one-dimensional operator. Then, we need to add a type conversion function to the implementation of the _ UNCC class to support the conversion to the wchar_t * type, this function is probably like this:
Operator wchar_t *();
So far, the implementation of the gasket class is clear at a glance.

The example can be changed to the following format:

# Include <iostream>
# Include <string>
Using namespace STD;

Class _
{
Public:

_ A (): m_p (null)
{
}

~ _ ()
{
If (null! = M_p)
{
Delete [] m_p;
}
}

_ A (string S): m_p (null)
{
M_p = new char [S. Length () + 1];
Strcpy (m_p, S. c_str ());
}

_ A (const char * s): m_p (null)
{
If (null = s)
{
M_p = new char [1];
* M_p = 0;
}
Else
{
M_p = new char [strlen (s) + 1];
Strcpy (m_p, S );
}
}

_ A (long s): m_p (null)
{
M_p = new char [9];
Sprintf (m_p, "% lD", S );
}

Operator char *()
{
Return m_p;
}

PRIVATE:

Char * m_p;

};

Void printstr (const char * s)
{
Cout <S <Endl;
}

Int main (INT argc, char * argv [])
{
String S ("I'm a C ++ string ");

Printstr (_ A (s ));
 
Printstr (_ A ("I'm a C ++ string "));

Printstr (_ A (4234234 ));

Return 0;
}

By the way, the gasket class is very useful. In the case of temporary type conversion, I suggest using the gasket class to avoid writing a large number of repeated type conversion codes, second, you can avoid Memory leakage. It is also recommended that the name of the gasket class be in the form of underline + uppercase class name to inform programmers that I am a gasket class.

Idle person, 2006.1.4

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.