问题描述
我是C ++的新手,试图弄清楚指针和引用之间的区别.我刚刚阅读了.
I'm new to C++ and trying to figure out the differences between pointer and reference. I've just read this short summary.
在文章中,作者提到day *operator++ (day *d);
不会编译(注意:day
是一个枚举类型),并指出此重载运算符函数的参数必须为T,T&或T const& ;,其中T是类或枚举类型.
In the article, the author mentioned that day *operator++ (day *d);
won't compile (note: day
is an enum type) and argued that the parameter for this overloaded operator function must be type T, T&, or T const&, where T is a class or enum type.
我假设指针是内置类型,而不是类或枚举,因此它不能用于重载运算符,并且对于所有内置类型(如int和double)来说,运算符重载都是不可能的.
I assume that pointer is a built-in type rather than a class or enum so it can't be used to overload operators and that operator overloading is not possible for all built-in types such as int and double.
例如,int i = 1; ++i;
永远不会通过重载类型为int
的++
运算符来使i
为3.
For example, int i = 1; ++i;
would never result in i
being 3 by overloading the ++
operator for the type int
.
我正确吗?请帮助我更好地理解这个问题.
Am I correct? Please help me understand this problem better.
推荐答案
运算符重载的第一个规则是:
您不能对内置数据类型重载运算符,只能对自定义数据类型重载,因此在这方面您是正确的.
First rule in Operator overloading is:
You cannot overload operators for built-in data types, You can only for your custom data types, So you are correct in that regard.
这篇关于C ++运算符重载将指针类型作为参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!