运算符重载和指针
大家好。
我很好奇。为什么我不能仅使用指向
类对象的指针来重载操作符?
例如:
class SomeClass {
friend SomeClass * operator<<(SomeClass *,int);
};
此代码将在G ++中给出以下错误消息:
错误:''SomeClass *运算符>>(SomeClass *,int)''必须有一个
类或枚举类型的参数
但是,如果我使用引用而不是指针,代码将编译
罚款。我想知道这个限制背后的原因。
谢谢!
Rg
Hello, everyone.
I''m curious. Why can''t I overload an operator only with pointers to
class objects?
For instance:
class SomeClass {
friend SomeClass *operator<<(SomeClass *, int);
};
This code will give me the following error message in G++:
error: ''SomeClass* operator>>(SomeClass*, int)'' must have an
argument of class or enumerated type
However, if I use references instead of pointers, the code will compile
fine. I''d like to know the reason behind that restriction.
Thanks!
Rg
Rg写道:
Rg wrote:
大家好。
我很好奇。为什么我不能仅使用指向
类对象的指针来重载操作符?
Hello, everyone.
I''m curious. Why can''t I overload an operator only with pointers to
class objects?
http://www.parashift.com/c++-faq-lit...html#faq-26.10
Gavin Deane
http://www.parashift.com/c++-faq-lit...html#faq-26.10
Gavin Deane
Rg写道:
Rg wrote:
大家好。
我很好奇。为什么我不能仅使用指向
类对象的指针来重载操作符?
Hello, everyone.
I''m curious. Why can''t I overload an operator only with pointers to
class objects?
你为什么想要?指针不是一个对象而是一个引用
是。
这里有一个关键的区别,因为引用不会接受
一个无效的对象但是指针将。
您可以将引用视为运行时检查。
顺便说一句,如果我理解您的请求,您已经有了一个
SomeClass指针:这个。
为什么不使用它?
Why would you ever want to? A pointer is not an object but a reference
is.
Thats a critical distinction here because a reference will not accept
an invalid object but a pointer will.
You can think of a reference as a runtime check.
Incidentally, if i understand your request, you already have a
SomeClass pointer: this.
Why not use that?
>
例如:
类SomeClass {
朋友SomeClass *运算符<<(SomeClass *,int);
};
此代码将在G ++中给出以下错误消息:
错误:''SomeClass *运算符>>(SomeClass *,int)''必须有一个类或枚举类型的
参数
>
For instance:
class SomeClass {
friend SomeClass *operator<<(SomeClass *, int);
};
This code will give me the following error message in G++:
error: ''SomeClass* operator>>(SomeClass*, int)'' must have an
argument of class or enumerated type
确切地说,指针不是一个对象。
Exactly, a pointer is not an object.
>
但是,如果我使用引用而不是指针,则代码将为co mpile
罚款。我想知道这种限制背后的原因。
>
However, if I use references instead of pointers, the code will compile
fine. I''d like to know the reason behind that restriction.
原因如上所述。它不是一个限制,它是一个特征。
这不是通过流注入整数的目标吗?
如果是这样,为什么不宣布并定义一个成员运算符<<需要一个
整数?
#include< iostream>
#include< ostream>
class SomeClass
{
int n;
public:
SomeClass() :n(0){}
void运算符<<(const int& r_n)//按值也可以
{
n = r_n;
}
朋友std :: ostream& operator<<(std :: ostream& os,SomeClass& r_sc)
{
return os<< r_sc.n;
}
};
int main()
{
SomeClass实例;
instance<< 5;
std :: cout<<实例<< std :: endl;
}
/ *
* /
>
或者我错过了什么?
The reason is as explained above. Its not a restriction, its a feature.
Isn''t the goal here to inject an integer via a stream?
If so, why not declare and define a member operator<< that takes an
integer?
#include <iostream>
#include <ostream>
class SomeClass
{
int n;
public:
SomeClass() : n(0) { }
void operator<<(const int& r_n) // by value is ok too
{
n = r_n;
}
friend std::ostream& operator<<(std::ostream& os, SomeClass& r_sc)
{
return os << r_sc.n;
}
};
int main()
{
SomeClass instance;
instance << 5;
std::cout << instance << std::endl;
}
/*
5
*/
Or did i miss something?
* Salt_Peter:
* Salt_Peter:
> ;
指针不是对象,而是引用。
>
A pointer is not an object but a reference is.
你完全倒退了。
-
A:因为它搞砸了人们通常阅读文字的顺序。
问:为什么这么糟糕?
A:热门帖子。
Q :usenet和电子邮件中最烦人的事情是什么?
You got that exactly backwards.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?