2202.cpp: Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
Class Test
{public
:/
*
Use a transformation constructor to convert the data of a specified type to an object of a class with a
type conversion function to convert an object of a class to a specified type of data
The general form of a type conversion function is:
operator type name ()
{The statement that
implements the transformation
}
cannot specify a function type before the function name, and the function has no parameters
The following example function name is: operator int
*
/Operator int ()
{return
m_inum;
}
Private:
int m_inum;
};
int _tmain (int argc, _tchar* argv[])
{
test test;
int m = test; This can be compiled through, if there is no type conversion function, the compilation error here, because the class type can not assign value to the int, once we define the
//type conversion function, it will implicitly call
int i;
cin>>i;
return 0;
}
Conversion constructors enable you to convert data of a specified type to an object of the class
You can convert an object of a class to a specified type of data by using a type conversion function
The general form of a type conversion function is:
operator type name ()
{The statement that implements the transformation}
Function type cannot be specified before function name, function has no arguments