-9

My professor ask me to find which operators in c++ can't overloaded and what is the reason for that. i found that dot(.),scope resolution (::),conditional (?:),sizeof() operators can't overloaded. can any one tell me the reason for that?

admdrew
  • 3,790
  • 4
  • 27
  • 39
  • 5
    http://www.stroustrup.com/bs_faq2.html#overload-dot – Marco A. Oct 08 '14 at 14:05
  • i go through stroustrup's websites yesterday. Explanations in that websites are too difficult for me to understand. If you can explain it more simply, please do it – Abhijith P Haridas Oct 08 '14 at 14:09
  • He is the creator of C++, why would you not want the answer straight from the horse's mouth? – Cory Kramer Oct 08 '14 at 14:10
  • i told already explanations in that website is to difficult for some one like me . – Abhijith P Haridas Oct 08 '14 at 14:13
  • @AbhijithPHaridas: I like his explanations. E.g. "Sizeof cannot be overloaded because built-in operations, such as incrementing a pointer into an array implicitly depends on it." Do you have specific questions? – tgmath Oct 08 '14 at 14:14
  • "incrementing pointer to an array implicitly depends on it" what does that means? i have an array a[10], if i do "a+2" how does it depends on size of operator? – Abhijith P Haridas Oct 08 '14 at 14:18
  • @AbhijithPHaridas How far the pointer, starting at the first element of the array, has to move depends on the size the elements inside. The +2 means, move the pointer `2*sizeof(T)` for type `T`. – tgmath Oct 08 '14 at 14:20
  • so when i do a+2 compiler implicitly use the sizeof operator?? is't it?? – Abhijith P Haridas Oct 08 '14 at 14:37

1 Answers1

0
struct Troll
{
  int money = 0;
  int problems = 0;
  float cant_touch_this = 0.0;

  int& operator.(const std::string& member_name)
  {
    if (member_name == "money")
      return problems;
    else if (member_name == "problems")
      return money;
    else if (member_name == "cant_touch_this")
      throw cant_touch_this;
    else
      throw 0;
  }
};

int main()
{
 Troll t;

 t.money = 42;
 t.problems = 3;
}

While writing the above snippet that definitely does not compile, i asked myself multiple questions:

  • What should operator. return type be ?
  • What parameter should it take ?
  • How do i handle the cases where i throw ?
  • Why do i need runtime overhead to evaluate a compile time member ?
  • Will other developers be fine with me trolling around by switching my members ?
  • This list could go on...

This is as much reasons as to why you cannot overload the dot(.) operator, and there are similar questions you would ask yourself by trying to overload the non overloadable operators.

A smart mind may find decent answers to these questions, but that mind is either not born yet, not a member of the c++ committee, not a fan of standard feature proposal or simply doesn't care because he doesn't need this feature.

Drax
  • 12,682
  • 7
  • 45
  • 85
  • but we can overload -> operator that used for a smiler purpose. why? – Abhijith P Haridas Oct 08 '14 at 14:48
  • @AbhijithPHaridas `operator->` is composed of `operator*` then `operator.`, when you overload `operator->` you only overload the `operator*` part of it, in fact you are forced to return a pointer which the compiler will dereference then use it's own native and godly powerful implementation of `operator.` See http://stackoverflow.com/q/4421706/1147772. – Drax Oct 08 '14 at 14:50