Is it different from foo.bar calling a function from a specific instance? I've seen it around on tutorials but it's never explained and it's too general a term to show up on google.
Asked
Active
Viewed 117 times
-2
-
1What happened when you tried replacing one with the other? – Kerrek SB Jun 04 '15 at 19:25
-
2`foo.bar` is invoking a method on an object, while `foo->bar` is de-referencing a pointer to an object, then invoking that method. – Austin Brunkhorst Jun 04 '15 at 19:25
3 Answers
1
Operator operator->
can only be used on a pointer type (in that case foo->bar
is equivalent to (*ptr).bar
) or a type that overloads operator->
(in that case the semantic depends on the overload itself).
An example with a pointer type might be:
struct some {
int x;
};
some a{10};
some* a_ptr = &x;
a.x = 10;
a_ptr->x = 10;
An example for an overloaded type could be:
std::unique_ptr<some> a_ptr = std::make_unique<some>(10);
a_ptr->x = 10;

Shoe
- 74,840
- 36
- 166
- 272
0
foo->bar
is short for (*foo).bar
. (Unless foo
is of some class type for which ->
does something else).

Baum mit Augen
- 49,044
- 25
- 144
- 182
0
foo->bar is used when foo is pointing to a certain data type foo.bar is used when foo is an object of a class

guest1234
- 5
- 2