且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

表达式必须具有类类型

更新时间:2021-12-23 21:58:19

它是一个指针,因此请尝试:

It's a pointer, so instead try:

a->f();

基本上是运算符(用于访问对象和引用使用对象的字段和方法),因此:

Basically the operator . (used to access an object's fields and methods) is used on objects and references, so:

A a;
a.f();
A& ref = a;
ref.f();

如果您使用的是指针类型,则必须先取消引用以获得引用:

If you have a pointer type, you have to dereference it first to obtain a reference:

A* ptr = new A();
(*ptr).f();
ptr->f();

a-&b; $ 表示法是通常只是(* a).b 的简写。

The a->b notation is usually just a shorthand for (*a).b.

operator-> 可以重载,这在智能指针中尤为明显。当您正在使用智能指针时>,那么您还可以使用-> 来引用指向的对象:

The operator-> can be overloaded, which is notably used by smart pointers. When you're using smart pointers, then you also use -> to refer to the pointed object:

auto ptr = make_unique<A>();
ptr->f();