« first day (827 days earlier)      last day (2025 days later) » 

5:52 AM
in c++ friend function operator overload, why do we return reference?
what is the reason
 
you don't
as in
if you return a reference, it's not because it's a friend function, rather, because of other reasons
 
6:22 AM
I mean its not necessary right
 
see above
 
6:42 AM
Also why cant we pass object as reference in friend function
is it to prevent changing of private members?
 
??????
where are you reading this
 
I was writing a program where friend function has object reference passed to it, but it gave error
 
go on
 
#include <iostream>
#include <cmath>

using namespace std;

class Complex {
	double real, imaginary;
public:
	double Real() {
		return real;
	}
	double Imaginary() {
		return imaginary;
	}
	Complex () {
		real = 0;
		imaginary = 0;
	}
	Complex (double re, double im) {
		real = re;
		imaginary = im;
	}
	Complex (const Complex &c) {
		real = c.real;
		imaginary = c.imaginary;
	}
	Complex operator+ (const Complex &c) const {
		Complex d;
		d.real = c.real + real;
		d.imaginary = c.imaginary + imaginary;
Here only see the moduluss
moduluss function
It gives error if Complex c is passed as reference
 
it gives you an error because
yesterday, by milleniumbug
temporaries don't bind to non-const lvalue references
 
6:51 AM
Oh my
I should bookmark this
 
also, I've said that before, but: accept a const reference if you aren't modifying the object
 
Ohhh c1-c2 is temporary but c3 is not so moduluss(c1-c2) gives error but moduluss(c3) does not!
 
indeed
 
In my example if I use all arguments as references in all friend function, is it bad code design?
I just dont want copy ctor to be called again and again
 
In something as simple as a Complex type, it may go both ways, as in, passing by reference may reduce or improve performance depending on various factors
oh and btw this one goes without saying, but for this class writing a copy constructor is absolutely unnecessary because the default compiler generated one does the right thing
 
7:04 AM
But default ctor is required to be written if we give parametrised constructor?
 
if you want the class to be default constructible
often you don't
 
 
4 hours later…
11:20 AM
int* a = {1, 2, 3, 4};
printf("%d", *(a+1));
works to give output as a[1]
my question is: isn't a pointer supposed to be an address? (if we print it using %p, we get a large hex value)
if so, how does adding 1 to an address make any sense in this case?
suppose the first address is 120
then the next would be 124 (assuming four byte int size)
then how does *(120 + 1) becomes *124?
 
when you add 1 to an int pointer it actually adds sizeof(int) to the actual value
it makes pointer arithmetic a lot easier in many cases, but trickier in a few others
 
how does it do so? do we say that the plus operator is "overloaded" i.e. it adds 1+1=2 in normal integers and *(120+1) = *124 in case of integer pointers?
 
the compiler knows that it's an int pointer because of the static type
 
but I was taught that in C we don't have function overloading or operator overloading :(
 
compiler can do a lot more that what you can do with the code
technically the arithmetic operators are overloaded to deal with float vs int anyways
 
11:27 AM
isn't *(a+1) just some code as well? like, the compiler could have also optimized for "string concatenation with integers" + 123
@ratchetfreak hmm good point
 
but in the backend the compiler treats pointer arithmetic specially, just like it treats float arithmetic differently from int arithmetic. Including stuff like implicit promotion
 
hmm
ok thanks
 
11:45 AM
@GaurangTandon By that they only meant you can't have your own custom operator definitions. Compiler can still do its own thing depending on the type of operands involved. (so the operators are effectively overloaded, but only for the several built-in types, with no other way to add your own)
Consider e.g. floating point. If compiler was forced to generate the same code for every type, 5.0 + 1.0 would have been broken because floating point has a vastly different representation than integers
So the same way you can say that adding 1 to an int* moves it by 1 element ahead, instead of 1 byte ahead, because that's just more useful
 
12:34 PM
alright, thanks @milleniumbug
 

« first day (827 days earlier)      last day (2025 days later) »