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

R

raj

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
 
K

Kapt. Boogschutter

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

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
 
R

Russell Hanneken

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

The second form involves more typing and requires more effort to read.
 
R

Richard Herring

In message said:
The second form involves more typing and requires more effort to read.
And they might have different effects if aa is of user-defined type.
Usually, operator->() returns (something that behaves like) a pointer;
operator*() returns a reference. Either or both might be some kind of
proxy object, not the object that aa ultimately "points" at. There's no
guarantee that they indirect to the same thing, or even that they are
both defined.
 
P

Peter Ammon

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

The reason is obvious: for compatibility with C.
 
D

Default User

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


The short answer to your question is, "because C did it that way." C++
was not about to redefine the usage. So why did C?

For the case above, the reply is obvious, syntactic sugar. The better
question is, why didn't C overload the . operator to work on either
struct/union objects or on pointers? For that, you'd have to ask Dennis
Ritchie. There probably is a good reason.



Brian Rodenborn
 
M

Mike Wahler

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.

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
 
J

jeffc

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.

The second is pretty awkward, but anyway, what would you do with this?
A aa;
 
J

jeffc

jeffc said:
The second is pretty awkward, but anyway, what would you do with this?
A aa;

Never mind, didn't read your question quite right. You are not implying
that we don't need ".".
 
T

tom_usenet

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.

You can separately overload operator-> and unary operator* for user
defined types. That's the only difference really, but aa->f() is much
nicer to read.

Tom
 
A

Andrey Tarasevich

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.
...

Well, any boolean function can be implemented by using logical operation
'xor' (or 'nor', or 'nand') and nothing else. Yet instead of single
'xor' we have 'and' (&&), 'or' (||) and 'not' (!) in the language. Why?
The make code easier to read. The same applies to '->' operator. In many
situations it produces more compact and easily readable code.

And it also gives us an additional overloadable operator.
 
J

JKop

Richard Herring posted:
And they might have different effects if aa is of user-defined type.
Usually, operator->() returns (something that behaves like) a pointer;
operator*() returns a reference. Either or both might be some kind of
proxy object, not the object that aa ultimately "points" at. There's no
guarantee that they indirect to the same thing, or even that they are
both defined.


Sounds like bullshit.

-JKop
 
A

Ali Cehreli

Richard Herring posted:

Sounds like bullshit.

-JKop

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';
}
 
J

JKop

Ali Cehreli posted:
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';
}


I stand corrected.

-JKop
 
V

Victor Bazarov

Default said:
raj wrote:





The short answer to your question is, "because C did it that way." C++
was not about to redefine the usage. So why did C?

For the case above, the reply is obvious, syntactic sugar. The better
question is, why didn't C overload the . operator to work on either
struct/union objects or on pointers? For that, you'd have to ask Dennis
Ritchie. There probably is a good reason.

C++ allows overloading of -> but not .

V
 
J

JKop

Victor Bazarov posted:
C++ allows overloading of -> but not .

He refering to how one can overload * .

If one overloads both * and -> for a class, and makes them different, then
the following is no longer equal:

SomeClass jk;

jk->Chocolate();

(*jk).Chocolate();


-JKop
 
R

Rob Williscroft

Victor Bazarov wrote in
in comp.lang.c++:
C++ allows overloading of -> but not .

I'm guessing that Default Users's point was, that given:

struct X
{
int x;
};

struct X xx, *xp = &x;

'C' could have done this:

Have xp.x be an int * pointing to xx.x and then *xp.x would have
derefrenced it (i.e. today's xp->x), no need for (*xp).x or xp->x.

I've encontered one C compiler that actually did this, no idea
wether it was a feature or a bug :).

Rob.
 
D

Default User

Victor said:
C++ allows overloading of -> but not .


But if there was no -> operator, then there would probably be a way to
overload the . operator.

I think that's a result of having two operators, not a cause. As in the
developer(s) didn't think to themselves, "It'd be greate to get rid of
that unnecessary -> but let's keep it so we don't have to allow
overloading the . operator."

The reason there's two is that C did it that way. As a consequence, C++
could take advantage by allowing overloads for one but not the other.



Brian Rodenborn
 

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,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top