CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2007
    Posts
    249

    Exclamation Why can not be overloaeded

    HI,
    I have some doubt. Why some of the operators can not be overloaeded.
    like .,::,*::, ? and sizeof.

    Is there any specific reason.
    Please someone explain
    Thanks

  2. #2
    Join Date
    Jan 2009
    Posts
    19

    Re: Why can not be overloaeded

    Its a text That I had copied from some white paper . hope this will help you

    Most operators can be overloaded by a programmer. The exceptions are

    . (dot) :: ?: sizeof

    There is no fundamental reason to disallow overloading of ?:. I just didn't see the need to introduce the special case of overloading a ternary operator. Note that a function overloading expr1?expr2:expr3 would not be able to guarantee that only one of expr2 and expr3 was executed.

    Sizeof cannot be overloaded because built-in operations, such as incrementing a pointer into an array implicitly depends on it. Consider:

    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.

    In N::m neither N nor m are expressions with values; N and m 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 appearances - involve introducing new syntax (to allow expr::expr). It is not obvious what benefits such a complication would bring.

    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 . For example:

    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?
    }

  3. #3
    Join Date
    Jul 2007
    Posts
    249

    Re: Why can not be overloaeded

    Still it is hard to understand.
    Any other explanation plz

  4. #4
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Why can not be overloaeded

    Still it is hard to understand. Any other explanation plz
    The overloads either make no sense (useless), or cause the meaning to become ambiguous.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured