-1

I am little bit confused with this code. main.cpp

int emptyApartments(const Apartment* apt, int num) {
   int sum = 0;
   for(int i = 0; i < num; i++) {
      sum += apt[i] && ~apt[i]; //this line is calling which functions?
   }
   return sum;
}

I am not sure line 4(main.cpp) is calling which functions from code below:

  Apartment(int number, double balance);
  void setEmpty();
  std::ostream& display()const;
  operator bool()const;
  operator int()const;
  operator double()const;
  bool operator~()const;
  Apartment& operator+=(double rent);
  Apartment& operator-=(double rent);
  Apartment& operator<<(Apartment& other);
  Apartment& operator>>(Apartment& other);
  Apartment& operator=(int number);
  Apartment& operator=(Apartment& other);
   
  friend double operator+(const Apartment& left, const Apartment& right);
  friend double operator+=(double& value, const Apartment& add);
Quimby
  • 17,735
  • 4
  • 35
  • 55
cine j
  • 1
  • You can use your debugger and step through the expression evaluation and see for yourself. Or add some debug prints. Have you tried that? What are your guesses? – Quimby Oct 12 '22 at 05:39
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems). Use a debugger. It is built for these kind of tasks. – Jason Oct 12 '22 at 05:41
  • I know that but debugger is not working that is the thing – cine j Oct 12 '22 at 05:44
  • How the debugger is not working? It is built for these kind of tasks. – Jason Oct 12 '22 at 05:46
  • I mean i am not able to figure out using debugger even though i am trying to find it from last 2 hours using debugger – cine j Oct 12 '22 at 05:52
  • Is `std::cout` broken *again* ? Hate when that happens. The answer to your question is going to be related to your overload unary op `bool operator~() const` being employed. Without that, ambiguity runs amok (in multiple ways). Suggest three well-placed `std::cout` writes to confirm your suspicion. – WhozCraig Oct 12 '22 at 05:55
  • I know that function is called, but along with that other operator function is calling I don't know that one – cine j Oct 12 '22 at 06:07
  • @cinej You put a breakpoint into each function and run the code with the debugger attached, it will stop when the breakpoints are hit. Or you can use "Step" feature all debuggers have. – Quimby Oct 12 '22 at 06:32
  • 1
    This isn't about the question, but... Wow this abstraction is broken. I am terrified of the different operators that are implemented because if I read code using this, I would have 0 idea what was happening – Human-Compiler Oct 12 '22 at 22:48

1 Answers1

0

C++ has lots of different bracket types, do not get them mixed up:

foo(x) is a function call.

foo[x] is for accessing an array.

(theres also <> and {}. Wait until you learn about lambdas :) )

In your example, apt is passed as a raw pointer - with the expectation that the callee is passing a fitting array with its length in num. Google "array decay" for more info on how this works: https://www.tutorialspoint.com/what-is-array-decay-in-cplusplus

As a note: in modern C++, avoid using native arrays. Use std containers (use std::vector if you can't decide)

EDIT: just re-reading you question: Is the tilde operator ~ the problem for understanding ? Try putting it's return value in a different temp variable to see what it does. (Spoiler: It's the binary NOT operation)

nick
  • 541
  • 1
  • 9
  • *"Is the tilde operator ~ the problem"* - There isn't a problem with the code; it's a problem with the OP's understanding (apparently of their own code). The posted code works. The OP isn't asking about array conversion to pointer-to-base works, nor how to call a function. They're asking which of the conversion operators (int, double, or bool) is being called, in addition to the overloaded `operator ~`. What they're seemingly doing everything-but-asking is why there isn't an ambiguous condition in that conversion operator resolution, since there are three possible overloads. – WhozCraig Oct 12 '22 at 17:00
  • @WhozCraig: Ok, yeah i see your point. My guess would be that it's not OPs own code but something they inherited though ;) As OP apparently doesn't care to clarify, i'll leave the answer as is for future readers. Can't make a horse drink etc... – nick Oct 13 '22 at 08:15