是 - >语法糖? [英] Is -> syntactic sugar?

查看:67
本文介绍了是 - >语法糖?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

表达式是


p->会员


相当于这个?


(* p).member


我有一个超载的类 - >和*。这是它的一部分。


class SharedPtr

{

Foo * p;

public :

Foo * operator->(){return p; }

Foo& operator *(){return * p; }

};


是否浪费了实施这两个操作员的工作,还是我真的需要

?说我写了


SharedPtr p;

p-> bar;


我删除operator-> ;从SharedPtr,该代码仍然会编译?编译器将它翻译成这个吗?


SharedPtr p;

(* p).bar;


感谢任何帮助。

Is the expression

p->member

equivalent to this one?

(*p).member

I have a class that overloads both -> and *. Here is part of it.

class SharedPtr
{
Foo *p;
public:
Foo *operator->() { return p; }
Foo &operator*() { return *p; }
};

Is it a waste of effort implementing both operators or do I actually need
both? Say I write

SharedPtr p;
p->bar;

and I remove operator-> from SharedPtr, will that code still compile? Does
the compiler translate it into this?

SharedPtr p;
(*p).bar;

Any help is appreciated.

推荐答案

当然它会编译...如果你有一个指向类的指针并且希望

来调用您使用的一个成员函数 - >......该类没有必要实现它。

实现它。


cmad
Of course it will compile... If you have a pointer to a class and want
to call one member function you use "->"... The class doesn''t have to
implement it.

cmad


2003年12月15日星期一20:39:26 +1100,Jason Heyes < GE ****** @ optusnet.com.au>写道:
On Mon, 15 Dec 2003 20:39:26 +1100, "Jason Heyes" <ge******@optusnet.com.au> wrote:
表达式

p->成员相当于这个?
(* p).member

我有一个超载两者的课程 - >和*。以下是其中的一部分。

class SharedPtr
{Foo * p;
公开:
Foo * operator->(){return p ; } foo& operator *(){return * p; }
};

是否浪费了实施两个操作员的努力,或者我真的需要两个吗?说我写了

SharedPtr p;
p-> bar;

我删除了operator->从SharedPtr,该代码仍然会编译?


你真的应该让自己成为一个C ++编译器。


使用Visual C ++ 7.1:

错误C2819:type ''SharedPtr''没有重载成员''运算符 - >''


编译器是否将其转换为此内容?

SharedPtr p;
(* p).bar;
Is the expression

p->member

equivalent to this one?

(*p).member

I have a class that overloads both -> and *. Here is part of it.

class SharedPtr
{
Foo *p;
public:
Foo *operator->() { return p; }
Foo &operator*() { return *p; }
};

Is it a waste of effort implementing both operators or do I actually need
both? Say I write

SharedPtr p;
p->bar;

and I remove operator-> from SharedPtr, will that code still compile?
You really should get yourself a C++ compiler.

Using Visual C++ 7.1:
error C2819: type ''SharedPtr'' does not have an overloaded member ''operator ->''

Does the compiler translate it into this?

SharedPtr p;
(*p).bar;




No.


以防万一这是作业(我发现令人难以置信的是你没有一个

编译器)你必须自己在std中查找相关的段落。



No.

Just in case this is homework (I find it incredible that you don''t have a
compiler) you''ll have to look up the relevant paragraphs in the std yourself.


Chris Mantoulidis在

新闻中写道:a8 ************************** @ posting.google .c om:
Chris Mantoulidis wrote in
news:a8**************************@posting.google.c om:
当然它会编译...如果你有一个指向一个类的指针并希望
调用一个成员函数你使用 - > ......班级没有必要实施它。

cmad


上面你看到的是引用,如只要它不是结束了

它可以帮助人们理解你在说什么。


在这种情况下:你似乎在回应:

表达式

p->成员

与此相当吗?

(* p).member

class SharedPtr
{Foo * p;
公开:
Foo * operator->(){return p ; } foo& operator *(){return * p; }
};

是否浪费了实施这两个操作员的努力,或者我是否真的需要两者兼顾?假设我写了


如果你想使用它,你需要实现operator - >()。

SharedPtr p;
p-> ; bar;

我删除了operator->来自SharedPtr,该代码是否仍然可以编译?
编译器是否将其转换为此内容?

SharedPtr p;
(* p).bar;
Of course it will compile... If you have a pointer to a class and want
to call one member function you use "->"... The class doesn''t have to
implement it.

cmad
What you see above is called quoting, as long as it isn''t over done
it helps people understand what you are talking about.

In this case: you seem to be responding to:
Is the expression

p->member

equivalent to this one?

(*p).member

I have a class that overloads both -> and *. Here is part of it.

class SharedPtr
{
Foo *p;
public:
Foo *operator->() { return p; }
Foo &operator*() { return *p; }
};

Is it a waste of effort implementing both operators or do I actually
need both? Say I write
You need to implement operator ->() if you want to use it.

SharedPtr p;
p->bar;

and I remove operator-> from SharedPtr, will that code still compile?
Does the compiler translate it into this?

SharedPtr p;
(*p).bar;




编号何时编译请参阅''p-> x。如果p是指针类型,它执行

内置成员访问,否则调用p.operator->()。它将继续执行此操作,直到operator->()返回指针时

它会对内置成员进行处理。


Rob。

-
http://www.victim-prime.dsl.pipex.com/


这篇关于是 - &gt;语法糖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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