当前位置:编程学习 > C/C++ >>

C++用户自定义转换(User-Defined Conversion)

 

在计算机语言中,类型的存在让我们可以更有针对性的进行数据和功能的处理,但是却又存在了类型转化的问题。C++如同其他计算机语言一样,也同样都这些问题。不过它相对于C而言多了引用类型(Reference);相对与C#来讲,又多了指针类型(Point)。这似乎让它的类型转化变得更加扑朔迷离。

也许是C方面知识的延续,我对C++的基础类型之间的转换还算比较清楚的,但是由于C#的Convert这个类库是那么的好用,以至于我一直不太适应C++的转换方式。不过经过导师的教授,发现C++的也可以构建禇类似Convert的转换方式。

在导师经过一堂课关于C++类型转换的课后,我又在网上查阅相关资料,以便更清晰详细的总结C++在类型转换的方式。

传统转换方式(Traditional Type-Casting)

C++作为C语言的超集,完全继承了C语言所具有的类型转换方法和能力,因此对于这部分在基础数值类型上的转换是比较容易理解的。但是因为C++是面向对象的语言,有类的概念,因此让又多一层需要理解的内容。

隐式转换(Implicit Conversion)

隐式转换不需要任何转换运算符,编译器会自动根据类型兼容性进行不同类型之间的转换。一般情况下,在C/C++中这种转换多出现在基本数值类型上,其基本原则就是所需内存小的类型可以直接转换成内存大相同的或者。

内存大小相同的类型之间也可以互相转换,但是得到的结果可能不是预期的,因而可能会得到编译器的警告。比如unsigned int uintVariable = -1; 。

虽说:程序员只在意错误(Error),不关心警告(Warning),但是导师也是严令禁止在程序中出现的,所以对于这样的隐式转换也会尽量避免。

显示转换(Explicit Conversion)

显示转换要表明所要转换的目标对象是什么样的类型,然后编译器会做出转换,它有两种格式:

•     C语言格式(C-like Cast)

(new_type) expression

•     函数式(Function-style Cast)

new_type (expression)

示例代码

#include <iostream>

using namespace std;

   

int main() {

    int x0 = 100;

    float num0 = x0;

    float num = 98.76;

    int x1 = (int) num;

    int x2 = int(num);

   

    cout << "num0 = " << num0 << endl;

    cout << "x1 = " << x1 << endl;

    cout << "x2 = " << x2 << endl;

    cout << "x3 = " << x3 << endl;

}

对于C++的类而言,也可以在其实例对象上使用传统的类型转换,这是利用了C++的一些语言特性。

下边就以例子来做解释

代码

#include<iostream>

#include<string>

using namespace std;

 

//macro definitions

#define  IDER_DEBUG 1

#define FUNC_TRAC(info) {if(IDER_DEBUG)cout<<"----"<<info<<"----"<<endl;}

 

//class declaration

class Human;

class Ape;

class Programmer;

 

//class definition

class Programmer

{

public:

    Programmer(string where = "genius")

    {

        FUNC_TRAC("Programmer Default Constructor");

        from = where;

    }

    /*Programmer(Programmer& p)

    {

        FUNC_TRAC("Programmer Copy Constructor");

        from = p.from;

    }*/

    void Speach(){cout<<"I am a Programmer, I am "<< from <<endl;}

private:

    string from;

};

 

class Human

{

public:

    Human(string where = "delivered by Parents"):heart("Human with Training")

    {

        FUNC_TRAC("Human Default Constructor");

        from = where;

    }

    Human(Ape& a):heart("Human with Practice")

    {

        FUNC_TRAC("Hummer Ape-Promotion Constructor");

        from = "Evolution from an Ape";

    }

    operator Programmer() //here is weird, it is really different whether we have "&" or not

    {

        FUNC_TRAC("Hummer Programmer-Cast Operator");

        return heart;

        //Programmer("Human with Practice"); // it is not good to return temporary variable

    }

    Human& operator =(Human& h)

    {

        FUNC_TRAC("Hummer Assignment Operator");

        cout<<"Call assignment"<<endl;

        return *this;

    }

    void Speach(){cout<<"I am a Human, I am "<< from <<endl;}   

private:

    string from;

    Programmer heart; //Every one has a heart to be a programmer

};

 

class Ape

{

public:

    Ape(string where = "from Nature")

    {

        FUNC_TRAC("Ape Default Constructor");

        from = where;

    }

    Ape& operator =(Programmer& p)

    {

        FUNC_TRAC("Ape Programmer-Assignment Operator");

        from="Degeneration from a Programmer";

        return *this;

    }

    /*Ape& operator =(Ape& p)

     {

        FUNC_TRAC("Ape Assignment Operator");

        cout<<"Ape assign"<<endl;

        return *this;

     }*/

    void Speach(){cout<<"#(*%^, !@#$&)( "<< from <<endl;}

private:

    string from;

};

 

 

//main function

int main(void) {

    Ape a;

    //a.Speach();

     Human h = a; // using promtion constructor

    //h.Speach();

   

    Human h2;

    h2 = a; // Error, no match assignment opeartor

   

    Programmer p = h; // using Programmer-cast operaotor

    //p.Speach();

    Programmer p0;

    p0 = h;    // using  Programmer-cast operaotor

   

    Prog

补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,