-1

in the below code (which is an example for type conversion):

// implicit conversion of classes:
#include <iostream>
using namespace std;

class A {};

class B {
public:
  // conversion from A (constructor):
  B (const A& x) {}
  // conversion from A (assignment):
  B& operator= (const A& x) {return *this;}
  // conversion to A (type-cast operator)
  operator A() {return A();}
};

int main ()
{
  A foo;
  B bar = foo;    // calls constructor
  bar = foo;      // calls assignment
  foo = bar;      // calls type-cast operator
  return 0;
}

in line 21: bar = foo; // calls assignment it calls the overloaded operator =. and in the body of class overloading for "=" operator is written in line 12: B& operator= (const A& x) {return *this;} it's statement says "return this". here this points to bar. and there's just a parameter of type class A: const A& x what does this function do with this parameter? and in line 21 what is converted to what by writing: bar = foo; ?

ali rahimi
  • 35
  • 3
  • 3
    "what does this function do with this parameter?" nothing, The parameter is unused – 463035818_is_not_an_ai Jun 23 '22 at 10:40
  • please one question per question – 463035818_is_not_an_ai Jun 23 '22 at 10:40
  • There's nothing for it to do, as neither `A` nor `B` have any data – Caleth Jun 23 '22 at 10:41
  • 1
    Does this answer your question? [What are the basic rules and idioms for operator overloading?](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading) – Richard Critten Jun 23 '22 at 10:41
  • Assignment operators don't convert anything, they copy things from one object to another but both objects already exist. – john Jun 23 '22 at 10:43
  • _"...what does this function do with this parameter?..."_ - nothing it's just skeleton code (as `class A` is empty and also `class B` contains no members). Usually you would write some code to do the assignment in a meaningful fashion copying member of `A` to `B` or even something else. – Richard Critten Jun 23 '22 at 10:44
  • 1
    There's not much special about an assignment operator, it's convention that makes you use them for assignment. The normal rules for functions also apply to any assignment operator you write, so try to understand the code above by thinking about the assignment operator as a regular function. – john Jun 23 '22 at 10:45
  • 1
    Just a remark, but specially in larger projects implicit conversions can be very tricky. They can be chained together and lead to unpredictable conversions. So in all code reviews I do, remove implicit type conversion opertains and make all constructors with one parameter explicit. The little pain now of typing explicit conversions where you want them may safe you a lot of pain later. – Pepijn Kramer Jun 23 '22 at 11:03
  • Since both classes are empty none of the code does anything at all. It's all just hot air. – Goswin von Brederlow Jun 23 '22 at 13:13

2 Answers2

2

The real question you might be asking is what does the operator= function used for?

Normally we take some data from the constant reference For example

struct A{
    std::string data;
}

struct B{
    std::string differentData;

    B& operator=(const A& other){
        differentData = other.data;
        return *this;
    }
}

Then if we call code like

int main(){
    A a;
    a.data = "hello";
    B b;
    b = a;

    std::cout << b.differentData;
}

We should get "hello" printing to the console

Since you're not interacting with the class given in the function, all this code is doing is running an empty function.

The return is only used if you chain together equal calls

int main(){
    B b1, b2, b3;

    b3.differentData = "hi";
    b1 = b2 = b3;
    //Pushes hi from b3 >> b2 and returns b2
    //Pushes hi from b2 >> b1 and returns b1
}
Natio2
  • 235
  • 1
  • 9
1

what does this function do with this parameter?

Nothing.

what is converted to what by writing: bar = foo;

Nothing. Or equivalently, the state of bar is replaced from the state of foo.

Caleth
  • 52,200
  • 2
  • 44
  • 75