在C ++中创建我自己的运算符 [英] Create my own operator in c++

查看:101
本文介绍了在C ++中创建我自己的运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道可以重载c ++中已经存在的运算符来定义所需的行为,但是可以创建自己的运算符吗?

I know it is possible to overload operators that already exist in c++ to define desired behavior, but is it possible to create your own operator?

例如,使运算符#返回容器的大小:

Just for example, making an operator # that returns the size of containers:

template<typename T>
size_t operator#(const T& obj) { return obj.size(); }

vector<int> v(1024);
cout << #v; // prints 1024

推荐答案

否.您需要坚持使用解析器已经知道如何处理的运算符.重载可以扩展表达式的含义,但是语法是固定的.

No. You need to stick with the operators the parser already knows how to handle. Overloading can extend the meaning of expressions, but the syntax is fixed.

这篇关于在C ++中创建我自己的运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆