重载()

#include <iostream>

using namespace std;
class Base
{
public:
    Base(int i){
        m_base=i;
    }
virtual void Display(const std::string &strShow = "Base class !")
    {
    std::cout <<"Base class !" << std::endl;
    std::cout <<"strShow:" << strShow<<std::endl;
    }

private:
    int m_base;
};
class Derive: public Base
{
    public:
    Derive(int i,int j):Base(i){
        m_drive=j;
    }
    virtual void Display(const std::string &strShow = "Derive class !")
    {
    std::cout << "Derive class !";
    std::cout <<"strShow:" << strShow<<std::endl;
    }
    operator int() const throw ()
    { return m_drive; }

protected:
    int m_drive;
};
int main()
{
    Derive derive(1,2);
    int test=(int)derive;///这里不是强制类型转换,是直接使用()操作符

    std::cout <<"test:" <<test<< std::endl;

    return 0;
}

  

原文地址:https://www.cnblogs.com/zhaobinyouth/p/15397077.html