krikara
krikara

Reputation: 2435

Different ways of calling functions C++

I know this is a very basic question, but after a few google searches and clicking on multiple links, I still couldn't find an answer.

My question is what is the difference between the " . " and the " -> " in function calling in C++ language?

For example, I have a program with 2 different data structures. Semaphore aaa; List bbb;

To use a function on semaphore, I have to do aaa.P(); but on List, I have to do List->append(object);

I don't quite understand why Semaphore uses .P() whereas List uses ->append() when semaphore is just a data structure containing an integer and a list.

Upvotes: 4

Views: 1267

Answers (6)

phunctor
phunctor

Reputation: 609

As far as your original question is concerned, the reason for the difference is that Semaphore is a class type, and List is a pointer type. This is a fact about your particular program, not so much about the language. Most likely there are some typedefs making this structural feature of your program one that is not immediately obvious.

Upvotes: 0

Praetorian
Praetorian

Reputation: 109119

Use the . operator when you're accessing members using the object itself, or a reference to the object.

struct Foo
{
  void bar() {}
};

Foo foo;
Foo& fooref = foo;

foo.bar();
fooref.bar();

Use the -> operator when you're accessing a member via a pointer to the object. Continuing the above example

Foo *fooptr = &foo;

fooptr->bar();
(*fooptr).bar();  // same as preceding line

Upvotes: 4

user541686
user541686

Reputation: 210402

On the surface:

a.b is used for accessing member b of object a.
a->b is defined to be eqivalent to (*a).b if a is a pointer type.

More info:

The . can't be overloaded, but -> can be overloaded for classes (non-pointer types). The return value of the -> operator is the result of applying the -> operator until a pointer type is reached.

Note that this is impossible to do with a sequence of dereference-followed-by-member-access operations (try it and see), even though the * (dereference) operator is overloadable. This is because you need to "loop" over the result of evaluating the return type of -> at compile-time, which is impossible to do manually. It's useful for making a proxy of an object that behaves like a pointer to the object.

Upvotes: 4

Zeenobit
Zeenobit

Reputation: 5194

In addition to what others said, foo->bar() is the exact same thing as (*foo).bar(). It's just a shorthand and convenient notation. That is unless the -> operator is overloaded for foo.

Upvotes: 0

Maxpm
Maxpm

Reputation: 25561

In Foo.Bar(), Foo is an object of a class that has Bar().

In Foo->Bar(), Foo is a pointer to an object that has Bar().

It's not so much a matter of which class uses which operator, but which operator to use depending on the context.

Upvotes: 2

paxdiablo
paxdiablo

Reputation: 881293

. means access an item within the object (for both data and functions). -> is the same but it uses a pointer to the object.

For example:

struct abc { int xyz; } a;
struct abc *pointer_to_a = &a;
a.xyz = 271828;                  // manipulates the object directly.
pointer_to_a->xyz = 314159;      // manipulates the object through a pointer.

Upvotes: 1

Related Questions