debug类的信息和typedef智能指针的用法

namespace FT {
    enum class LD : int32_t {
        B = 0,
        P = 1,
        N = 2
    };

    struct debug_test {
        LD one;
        string two;
        int three;

        debug_test(LD a, string b, int c) : one(a), two(b), three(c) {}
    };

}

string toString(FT::LD const e) {
    switch (e) {
        case FT::LD::B: {
            return std::string("bbb");
        }
        case FT::LD::N: {
            return string("nnn");
        }
        case FT::LD::P: {
            return string("ppp");
        }
        default: {
            return string("default");
        }
    }
}


namespace FT {
	//用于将LD信息转化成string的信息后可以输入给输出信息
    inline std::ostream &operator<<(std::ostream &os, LD const &ld) {
        return os << ::toString(ld);
    }
	
	//将全部内容都转化成一个stringstream流,这样子可以输出
    inline std::ostream &operator<<(std::ostream &os, debug_test const &dt) {
        os << "LD(";
        os << dt.one;
        os << ")"<<std::endl;
        os << "string(";
        os << dt.two;
        os << ")"<<std::endl;
        os << "int(";
        os << dt.three;
        os << ")";
    }
}

namespace std {
    inline std::string to_string(FT::LD const &ld) {
        return toString(ld);
    }

    inline std::string to_string(FT::debug_test const &dt) {
        stringstream sstream;
        sstream << dt;
        return sstream.str();
    }
}

struct AA {
    typedef std::shared_ptr<AA> Ptr;
    int a;

    AA(int _a) : a(_a) {}
};

//主函数,可以自己定义在std中的to_string函数,只要输入的内容是标准库中没有的就可以了,一般自己定义的结构体标准库中也不会有的
int main() {
	//debug自己信息
    FT::debug_test dt(FT::LD::P, "hello", 0);
    cout << std::to_string(dt) << endl;
    //使用自己定义的智能指针类型
    AA::Ptr A_ptr(new AA(5));
    cout << A_ptr->a << endl;
    return 0;
}

使用#if的方式进行调试和注释最简单的例子

#if condition
  code1
#else
  code2
#endif

例子
#if 1
    cout<<"debug"<<endl;
#else
    cout<<"real"<<endl;
#endif

可以首先将condition写成1,在code1写需要调试的代码,在调试完毕后,将condition变成0,就可以使用code2中的代码

在自己的代码中重载标准库中的内容的时候的写法,需要返回类的绝对值的时候其实是部分的绝对值,返回的是有这种绝对值的内容的对象,所以整体的写法是

namespace FT {
    class aaa {
    public:
        explicit aaa(double const maa_) : maa(maa_) {}

        explicit operator double () const
        {
            return maa;
        }


    private:
        double maa;
    };
}

namespace std{
    inline ::FT::aaa fabs(const ::FT::aaa other){
        ::FT::aaa const result(std::fabs(static_cast<double >(other)));
        return result;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值