Topic: Implicit conversion before dot
Author: erc@netcom.com (Eric Smith)
Date: Wed, 9 Jun 1993 09:57:47 GMT Raw View
Can a class object be implicitly converted to a non-base class to use
the '.' operator? For example:
class A {
public:
void f();
};
class B {
public:
operator A();
};
void g() {
B b;
b.f(); // Equivalent to b . operator A() . f();
}
Author: steve@taumet.com (Steve Clamage)
Date: Wed, 9 Jun 1993 17:00:39 GMT Raw View
erc@netcom.com (Eric Smith) writes:
>Can a class object be implicitly converted to a non-base class to use
>the '.' operator? For example:
>class A {
> public:
> void f();
>};
>class B {
> public:
> operator A();
>};
>void g() {
> B b;
> b.f(); // Equivalent to b . operator A() . f();
>}
No. Given
expr . name
the 'expr' must evaluate to a class object which has 'name' as a member
(see 5.2.4). You must use an explict conversion in this case:
(b.operatorA()).f()
((A)b).f()
--
Steve Clamage, TauMetric Corp, steve@taumet.com
Author: dougm@titan.cs.rice.edu (Doug Moore)
Date: Wed, 9 Jun 1993 21:58:40 GMT Raw View
>>>>> On Wed, 9 Jun 1993 17:00:39 GMT,
Steve> steve@taumet.com (Steve Clamage):
Eric> erc@netcom.com (Eric Smith):
Eric>Can a class object be implicitly converted to a non-base class to use
Eric>the '.' operator? For example:
Steve> No. Given
Steve> expr . name
Steve> the 'expr' must evaluate to a class object which has 'name' as a member
Steve> (see 5.2.4). You must use an explict conversion in this case:
Steve> (b.operatorA()).f()
Steve> ((A)b).f()
Related question. Can a class object be implicitly converted to a
pointer to use either the -> or ->* operators?
If so, why overload operator -> for pointer smartness? Just define a
pointer conversion.
If not, how can one use ->* with smart pointers?
In either case, compilers disagree, especially with respect to ->*.
Doug Moore
(dougm@cs.rice.edu)
Author: steve@taumet.com (Steve Clamage)
Date: Thu, 10 Jun 1993 17:26:29 GMT Raw View
dougm@titan.cs.rice.edu (Doug Moore) writes:
>Related question. Can a class object be implicitly converted to a
>pointer to use either the -> or ->* operators?
The question makes no sense. Given the expression
a->b
1. If 'a' is a pointer type, only the built-in "->" is meant, and 'a'
must point to an object of a type with a member 'b'. You cannot
change the built-in meaning of operators, but only extend them to
apply to class types.
2. If 'a' is not a pointer type, it must be an object of a type with
operator-> defined as a non-static member function. In that case,
that member function is called, and the result is (ARM 13.4.6)
(a.operator->())->b // then re-apply steps 1 - 3
3. If neither is the case, the expression is invalid.
The same considerations apply to "operator->*".
You can read about this in any good C++ text.
--
Steve Clamage, TauMetric Corp, steve@taumet.com
Author: maxtal@physics.su.OZ.AU (John Max Skaller)
Date: Fri, 11 Jun 1993 07:35:12 GMT Raw View
In article <1993Jun10.172629.23165@taumet.com> steve@taumet.com (Steve Clamage) writes:
>dougm@titan.cs.rice.edu (Doug Moore) writes:
>
>>Related question. Can a class object be implicitly converted to a
>>pointer to use either the -> or ->* operators?
>
>The question makes no sense. Given the expression
> a->b
>
>1. If 'a' is a pointer type, only the built-in "->" is meant, and 'a'
>must point to an object of a type with a member 'b'. You cannot
>change the built-in meaning of operators, but only extend them to
>apply to class types.
>
>2. If 'a' is not a pointer type, it must be an object of a type with
>operator-> defined as a non-static member function. In that case,
>that member function is called, and the result is (ARM 13.4.6)
> (a.operator->())->b // then re-apply steps 1 - 3
>
>3. If neither is the case, the expression is invalid.
>
>The same considerations apply to "operator->*".
>
>You can read about this in any good C++ text.
Beg to differ Steve: ARM p338, on operator->
"Note that there is nothing special about operator->*.
The rules in this section apply only to operator->"
That is, ->* is an ordinary binary operator, whereas -> is
a unary operator (when overloaded).
--
JOHN (MAX) SKALLER, INTERNET:maxtal@suphys.physics.su.oz.au
Maxtal Pty Ltd, CSERVE:10236.1703
6 MacKay St ASHFIELD, Mem: SA IT/9/22,SC22/WG21
NSW 2131, AUSTRALIA