zoukankan      html  css  js  c++  java
  • 【旧博文】operator 类名()的用法

    在WINDOWS编程中有很多这样的情况,我定义一个句柄直接与窗体进行相等运算例如伪代码如下;

     HAND h;//句柄

     FORMINFO fi;//窗体类

    。。。。。。。

    h = fi;

    之后h就是窗体类的句柄。这种技术广泛应用与window的编程中。我们要问,两个毫不相干的类,怎么可以直接相等呢?看一下代码

    #include <iostream>

    using namespace std;

    class LCD

    {

    public:

           void info()

           {

                  cout<<"这是显示器:"<<id<<endl;

           }

        int id;

    };

    class Computer//电脑

    {

    public:

    Computer()

    {

       _lcd.id = count++;

    }

           operator LCD()

           {

                  return this->_lcd;

           }

           void info()

           {

                  cout<<"这是电脑"<<endl;

           }

    protected:

           LCD _lcd;

           static int count;

    };

    int Computer::count = 0;

    int main()

    {

           Computer c;

           Computer d;

           Computer e;

           c.info();

           LCD lcd = e;

           LCD lc = d;

           lcd.info();

           lc.info();

           return 1;

    }

    关键是

           operator LCD()

           {

                  return this->_lcd;

           }

    的使用实现了可以直接赋值。

    你也可以扩展这个类,使其可以返回很多类型。

  • 相关阅读:
    POJ3259 Wormholes
    leetCode-Plus One
    leetCode-Pascal's Triangle
    leetCode-Longest Continuous Increasing Subsequence
    leetCode-Missing Number
    leetCode-Maximum Product of Three Numbers
    leetCode-Image Smoother
    leetCode-Contains Duplicate
    机器学习实战笔记-使用Apriori算法进行关联分析
    leetCode-Degree of an Array
  • 原文地址:https://www.cnblogs.com/ldr123/p/2389607.html
Copyright © 2011-2022 走看看