Похожие чаты

#Include <iostream> using namespace std; struct complexCls { float

real,imag;
complexCls(float real = 0,float imag =0) :
real{real},imag{imag}{}

complexCls operator+(const complexCls& cls) {
return complexCls {
real+cls.real,
imag+cls.imag
};
}

};


int main() {

complexCls cls = 0 + complexCls{};

return 0;
}

why 0 can't implicitly cast to complexCls and then call + over that ?

10 ответов

17 просмотров

Do you know what is *this in your operator implementation?

Mehrad- Автор вопроса
Azadi
Do you know what is *this in your operator impleme...

i don't want to mutate my object, so i didn't use that

Mehrad
i don't want to mutate my object, so i didn't use ...

No, you didn’t get my point. I want to direct you to the answer not give it directly to you. Since the function is a member one, there’s a ‘this’ pointer there. Try to figure out what it points to in your implementation and if that makes sense.

Mehrad- Автор вопроса
Azadi
No, you didn’t get my point. I want to direct you ...

if i have a type T then "this" is T* or const T* did you mean what the meanning of "this" was ?

For what I know there are two ways to declare operators: - as member function - as free function (not all operators allow non-member version) And only non-member ones allow to convert the first operand as in the case you described. This also means that non-member operators can bit-by-bit slow down the compilation (as the compiler would need to consider more of them for each operator call), so declaring all operators as non-member functions is not a good idea. More about overloading operators: https://en.cppreference.com/w/cpp/language/operators More about how the call being resolved: https://en.cppreference.com/w/cpp/language/overload_resolution

Mehrad- Автор вопроса
Pavel
For what I know there are two ways to declare oper...

"And only non-member ones allow to convert the first operand as in the case you described." i didn't understand why only non-members are allowed to cast implicity ? why the compiler can't detect the exact operator+ which called by the oher arguments we passed ? (in this case the second complex litteral complexCls{})

Mehrad
"And only non-member ones allow to convert the fir...

I'm not an expert in this question, but basically what I said above, if every operator bahaved as non-member operator, then for every operator call you would need to check all the operators of that kind ever defined in your code and in the libraries and find which can convert to the class where you defined it. It sounds pretty inefficient.

Pavel
For what I know there are two ways to declare oper...

Apart from those two, you can also have a friend operator overloading inside class all of which have their specifications to prefer one over the other. Where does that bit-by-bit slow compilation come from? Haven't seen it anywhere. The problem with the OP's use case is that 0 is considered to be of the type of the struct to which this points inside the operator function. Using complexCls cls = complexCls{} + 0; would omit the problem.

Azadi
Apart from those two, you can also have a friend o...

I don't remember exactly but there was a talk that mentioned to not create operator overloads as free functions because of this issue

Pavel
I don't remember exactly but there was a talk that...

What would be the choice if the operator is binary?

Похожие вопросы

Обсуждают сегодня

Господа, а что сейчас вообще с рынком труда на делфи происходит? Какова ситуация?
Rꙮman Yankꙮvsky
29
А вообще, что может смущать в самой Julia - бы сказал, что нет единого стандартного подхода по многим моментам, поэтому многое выглядит как "хаки" и произвол. Короче говоря, с...
Viktor G.
2
@Benzenoid can you tell me the easiest, and safest way to bu.y HEX now?
Živa Žena
20
This is a question from my wife who make a fortune with memes 😂😂 About the Migration and Tokens: 1. How will the old tokens be migrated to the new $LGCYX network? What is th...
🍿 °anton°
2
30500 за редактор? )
Владимир
47
а через ESC-код ?
Alexey Kulakov
29
What is the Dex situation? Agora team started with the Pnetwork for their dex which helped them both with integration. It’s completed but as you can see from the Pnetwork ann...
Ben
1
Гайс, вопрос для разносторонее развитых: читаю стрим с юарта, нада выделять с него фреймы с определенной структурой, если ли чо готовое, или долбаться с ринг буффером? нада у...
Vitaly
9
Anyone knows where there are some instructions or discort about failed bridge transactions ?
Jochem
21
@lozuk how do I get my phex copies of my ehex from a atomic wallet, to move to my rabby?
Justfrontin 👀
11
Карта сайта