类型转换函数转换为构造类型

/*类型转换函数一般用于将类型转换为基本数据类型,但也可以转换为构造类型,如下*/
#include <stdlib.h>
#include<iostream>
#include <stdio.h>
#include<cmath>
using namespace std;
class Point
{
private:
 double x;
public:
 Point(double a= 0):x(a){};
 void Show(){cout<<"x:"<<x<<endl;}
};
class A
{
protected:
 double a;
public:
 A(double x =0):a(x){}
 double Geta(){return a;}
};
class B: public A
{
public:
 B(double x= 0):A(x){}
 operator Point(){
  return Point(Geta());
 }
};
int main()
{
 B obj1(18);
 Point obj2=obj1;//可以隐式调用
 //Point obj2=obj1.operator Point();//也可以显示调用
 obj2.Show();
 return 0;
}

 

posted @ 2012-10-11 22:33  myth_HG  阅读(232)  评论(0编辑  收藏  举报