c++中operator

#include "iostream"
#include "sstream"
#include "typeinfo"
using namespace std;


template <typename T> T calc(const T a,const T b){
    T c = a + b;
    return c;
}


class DebugDelete{
public:
    DebugDelete(std::ostream &s = std::cout):os(s){

    }

    template <typename T> void operator()(const T &t) const{
        std::cout <<  "operator  t = " << t << std::endl; 
        // delete p;
    }

    template <typename T> void operator=(const T &t) const{
        // os << "equal unique_ptr" << std::endl;
        std::cout <<  "operator equal t = " << t << std::endl; 
        // delete p;
    }

private:
    std::ostream &os;
};

int main(){

    auto e = calc(83,7);
    std::cout << "e = " << e << std::endl;

    auto s = calc(std::string("what "), std::string("can i do "));
    std::cout << "s = " << s << std::endl;

    auto t = 98;
    std::cout << "t = " << typeid(t).name() << std::endl; 
    auto ts = "my str";
    std::cout << "ts = " << typeid(ts).name() << std::endl; 

    DebugDelete del;
    del(837);
    del(std::string("delete mystr"));
    del("i am a char*");

    del = "i am a char*";

    return 0;
}

结果:

$ ./a.exe
e = 90
s = what can i do
t = i
ts = PKc
operator  t = 837
operator  t = delete mystr
operator  t = i am a char*
operator equal t = i am a char*

 

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页