Operator . (dot) could in principle be overloaded using the same technique
as used for ->.
However, doing so can lead to questions about whether an operation is meant
for the
object overloading. or an object referred to by .
class Y {
public:
void f();
// ...
};
class X { // assume that you can overload .
Y* p;
Y& operator.() { return *p; }
void f();
// ...
};
void g(X& x)
{
x.f(); // X::f or Y::f or error?
}
In ClassName::Memb er neither ClassName nor Member are expressions with
values;
ClassName and Member are names known to the compiler and :: performs a
(compile time)
scope resolution rather than an expression evaluation.
One could imagine allowing overloading of x::y where x is an object rather
than a namespace or a class,
but that would contrary to first appearences - involve introducing new
syntax (to allow expr::expr).
?: is not allowed to overload because,
A function overloading expr1? expr2:expr3 would not be able to guarantee
that
only one of exper2 and expr3 was executed as its in the hand of programmer.
sizeof is not allowed to overload because,
A function overloading sizeof can return anything they want..!!!!
Sizeof cannot be overloaded because built-in operations, such as
incrementing a pointer into an array implicitly depends on it.
X a[10];
X* p = &a[3];
X* q = &a[3];
p++; // p points to a[4] thus the integer value of p must be sizeof(X)
larger than the integer value of q
Thus, sizeof(X) could not be given a new and different meaning by the
programmer without violating basic language rules.
<am*********@gm ail.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
Hi,
As you know there are few operators in C++ which cant be overloaded.
They are:
., .*, ::, ?: , new , delete , sizeof , typeid , static_casr ,
dynamic_cast , const_cast , reinterpret_cas t .
Theremust be some reason for this restriction for each of the
operators.
Does anyone know the exact reason for such restriction on each of them?