赋值运算符是否适用于不同类型的对象?

时间:2021-01-21 22:29:14
class A {
public:
void operator=(const B &in);
private:
 int a;
};

class B {
private:
 int c;

}

sorry. there happened an error. is assignment operator valid ? or is there any way to achieve this? [There is no relation between A and B class.]

抱歉。发生了一个错误。赋值运算符有效吗?或者有什么方法可以达到这个目的吗? [A和B级之间没有关系。]

void A::operator=(const B& in) 
{ 
a = in.c;

} 

Thanks a lot.

非常感谢。

4 个解决方案

#1


Yes you can do so.

是的,你可以这样做。

#include <iostream>
using namespace std;

class B {
  public:
    B() : y(1) {}
    int getY() const { return y; }
  private:
     int y;
};


class A {
  public:
    A() : x(0) {}
    void operator=(const B &in) {
       x = in.getY();
    }
    void display() { cout << x << endl; }
  private:
     int x;
};


int main() {
   A a;
   B b;
   a = b;
   a.display();
}

#2


Both assignment operator and parameterized constructors can have parameters of any type and use these parameters' values any way they want to initialize the object.

赋值运算符和参数化构造函数都可以具有任何类型的参数,并以任何想要初始化对象的方式使用这些参数的值。

#3


This isn't an answer, but one should be aware that the typical idiom for the assignment operator is to have it return a reference to the object type (rather than void) and to return (*this) at the end. This way, you can chain the assignent, as in a = b = c:

这不是一个答案,但是应该意识到赋值运算符的典型习惯是让它返回对象类型的引用(而不是void)并在结尾返回(* this)。这样,您可以链接分配,如a = b = c:

A& operator=(const A& other) 
{
    // manage any deep copy issues here
    return *this;
}

#4


Others have clued in on this, but I'll actually state it. Yes you can use different types, but note that unless you use friend, your class cannot access the private members of the class it's being passed in with the operator.

其他人已经对此有所了解,但我实际上会说出来。是的,您可以使用不同的类型,但请注意,除非您使用朋友,否则您的班级无法访问与运营商一起传递的类的私有成员。

Meaning A wouldn't be able to access B::c because it's private.

含义A无法访问B :: c,因为它是私有的。

#1


Yes you can do so.

是的,你可以这样做。

#include <iostream>
using namespace std;

class B {
  public:
    B() : y(1) {}
    int getY() const { return y; }
  private:
     int y;
};


class A {
  public:
    A() : x(0) {}
    void operator=(const B &in) {
       x = in.getY();
    }
    void display() { cout << x << endl; }
  private:
     int x;
};


int main() {
   A a;
   B b;
   a = b;
   a.display();
}

#2


Both assignment operator and parameterized constructors can have parameters of any type and use these parameters' values any way they want to initialize the object.

赋值运算符和参数化构造函数都可以具有任何类型的参数,并以任何想要初始化对象的方式使用这些参数的值。

#3


This isn't an answer, but one should be aware that the typical idiom for the assignment operator is to have it return a reference to the object type (rather than void) and to return (*this) at the end. This way, you can chain the assignent, as in a = b = c:

这不是一个答案,但是应该意识到赋值运算符的典型习惯是让它返回对象类型的引用(而不是void)并在结尾返回(* this)。这样,您可以链接分配,如a = b = c:

A& operator=(const A& other) 
{
    // manage any deep copy issues here
    return *this;
}

#4


Others have clued in on this, but I'll actually state it. Yes you can use different types, but note that unless you use friend, your class cannot access the private members of the class it's being passed in with the operator.

其他人已经对此有所了解,但我实际上会说出来。是的,您可以使用不同的类型,但请注意,除非您使用朋友,否则您的班级无法访问与运营商一起传递的类的私有成员。

Meaning A wouldn't be able to access B::c because it's private.

含义A无法访问B :: c,因为它是私有的。