why have both "." and "->" ?

H

Howard

Kapt. Boogschutter said:
There is a difference I believe that xxxx->yyyy is used if xxxx is a pointer
to an object/class and xxxx.yyyyy if xxxx is the object/classs

That's true, but look at the question again. The poster explicitly used
(*aa).f(), and that's using a de-referenced pointer, which is the same thing
as your xxxx.yyyy example.

His question was why the "aa->f()" form is needed if you can accomplish it
using "(*aa).f()".

I would guess it's a convenience. It's sure easier to type, in my opinion!

-Howard
 
S

Steven T. Hatton

raj said:
I used to remember why c++ needed both ?
Could somebody help me here ?

For example

class A{
f();
};

A* aa;

You could do either "aa->f()" or "(*aa).f()". So why does C++ need both
operators.

Raj

After reviewing some of the replies to your question, you may be asking "Why
are people in this news group so obnoxious, condescending, and rude?". I
certainly am asking, but I can't answer that question.
 
S

Steven T. Hatton

Mike said:
operators.

The -> operator is not technically necessary, it's just
a 'shorthand' notation for (*).

Use whichever you like, but keep in mind that ->
is typically considered more 'idomatic' (i.e.
most coders will recognize it, and often makes
reading code faster.)

-Mike

Now that's an answer worth providing.
 
A

a

Ali Cehreli said:
But it is not:

struct Type0
{
int foo() const
{
return 42;
}
};

struct Type1
{
int foo() const
{
return 7;
}
};

struct Proxy
{
Type0 type0_;
Type1 type1_;

Type0 & operator* ()
{
return type0_;
}

Type1 * operator-> ()
{
return &type1_;
}
};

#include <iostream>

int main()
{
Proxy p;
std::cout << (*p).foo() << '\n';
std::cout << p->foo() << '\n';
}

Thats not the same behavior the original poster was asking about. Of course
if you overload the operators to do different things they will behave
differently.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top