C ++将一个对象的值复制到另一个对象中

时间:2022-09-24 18:48:37

I have

Class A
{
    A(bool t1, bool t2, bool t3......................bool tn );
}

and

Class B
{
    B(bool t1, bool t2, bool t3.......................bool tn);
}

Both classes have same structure , I want to extract all bool values from Class A obj1 and pass it to Class B obj2 .

两个类都有相同的结构,我想从类A obj1中提取所有bool值并将其传递给Class B obj2。

If I get value of each bool variable from obj1 and pass it to obj2 ... it will make code very huge .

如果我从obj1获得每个bool变量的值并将其传递给obj2 ......它将使代码非常庞大。

I used reinterpret_cast to solve this problem however I got c++ lint warning 826 http://stellar.cleanscape.net/products/cpp/checks.html .

我使用reinterpret_cast来解决这个问题但是我得到了c ++ lint警告826 http://stellar.cleanscape.net/products/cpp/checks.html。

4 个解决方案

#1


2  

Pass a std::vector as argument holding bool values from 1 to n. Why do even you have so much single variables instead of a vector?

传递一个std :: vector作为参数,将bool值从1保持为n。为什么即使你有这么多单变量而不是矢量?

Make

B(bool t1, bool t2, bool t3.......................bool tn);

To

B(std::vector<bool>& booleanValues);

Pass booleanValues to A so you get this code:

将booleanValues传递给A,以便获得此代码:

B(std::vector<bool>& booleanValues)
{
     A(booleanValues);
}

#2


2  

You could create a constructor for class b that takes class a as a parameter, in the constructor you would do the assignment. Or you could overload the = operator. Ultimately you could do:

您可以为类b创建一个构造函数,它将类a作为参数,在构造函数中进行赋值。或者你可以重载=运算符。最终你可以这样做:

class b = class a;

if you take the route of overloading the = operator.

如果你采取重载=运算符的路线。

You can read about the copy constructors and operator = here: http://www.learncpp.com/cpp-tutorial/911-the-copy-constructor-and-overloading-the-assignment-operator/

您可以在这里阅读有关复制构造函数和operator =的信息:http://www.learncpp.com/cpp-tutorial/911-the-copy-constructor-and-overloading-the-assignment-operator/

A word of caution, make sure to read the article if you choose the = operator route because it takes some getting used to.

需要注意的是,如果您选择=运算符路由,请务必阅读该文章,因为它需要一些时间来习惯。

#3


1  

well, the real question is weather the classes are the same, have the same struct, have some parts that are the same or is one of them is kind of the other kind.

好吧,真正的问题是天气类是相同的,有相同的结构,有一些相同的部分或其中一个是另一种。

  • same class: use only 1 class and create as much instance as you need
  • 同一个类:只使用1个类,并根据需要创建尽可能多的实例

  • same struct: if they have the same struct but different methods you can make a parent class or leave them as they are, cus' same struct doesn't necessarily say same meaning.
  • 相同的结构:如果它们具有相同的结构但是具有不同的方法,则可以创建父类或保持它们原样,cus'相同的结构不一定表示相同的含义。

  • are the same or is one of them is kind of the other kind: this probably say inheritance. you'll need to decide based on logic which parts are supposed to go to the parent class and weather the parent class should be abstract, or i A parent of B, the other way around or a new parent class for both
  • 是相同的还是其中一种是另一种:这可能是继承。你需要根据逻辑决定哪些部分应该转到父类,并且父类的天气应该是抽象的,或者我是B的父类,反过来或两者的新父类

#4


1  

You can do copy constructor (and also conversion constructors) and operator= as mentioned in another answer, and put all the bools in an array as also mentioned in another answer. If you need or want the bools to have discrete variable names, you can put them in a struct and use that struct to pass the values. This code just passes the struct around with a Get() and Set(): If you do a copy constructor and operator= the struct isn't necessary since the large assignment code is hidden in member functions and not seen when passed from object to object. But it may be best to retain it if the different types of objects will always contain the same set of bools and the bools are all related in some way.

您可以复制构造函数(以及转换构造函数)和operator =,如另一个答案中所述,并将所有bool放入数组中,如另一个答案中所述。如果您需要或希望bool具有离散变量名称,您可以将它们放在结构中并使用该结构来传递值。这段代码只是通过Get()和Set()传递结构:如果你做一个复制构造函数和operator =没有必要的结构,因为大的赋值代码隐藏在成员函数中,而在从对象传递到目的。但如果不同类型的对象总是包含同一组bool并且bool都以某种方式相关,那么最好保留它。

#include <iostream>
using namespace std;

struct States {
  bool left;
  bool right;
  bool up;
  bool down;
};

class A {
  States states;
public: 
  A (bool left, bool right, bool up, bool down) : states {left,right,up,down} {}
  void Print(void) { 
    cout << states.left << ',' << states.right << ',' << states.up << ',' << states.down << endl;
  }
  void Set(const States &states) { this->states = states; }
  const States &Get(void) { return states; }
};

class B {
  States states;
public:
  B (bool left, bool right, bool up, bool down) : states {left,right,up,down} {}
  void Set(const States &states) { this->states = states; }
  const States &Get(void) { return states; }
  void Print(void) { 
    cout << states.left << ',' << states.right << ',' << states.up << ',' << states.down << endl;
  }
};

int main()
{
  A a(true,false,true,false);
  a.Print();
  B b(true,true,true,true);
  b.Print();
  b.Set( a.Get() );
  b.Print();
  B bb(false,false,false,false);
  a.Set( bb.Get() );
  a.Print();
}

#1


2  

Pass a std::vector as argument holding bool values from 1 to n. Why do even you have so much single variables instead of a vector?

传递一个std :: vector作为参数,将bool值从1保持为n。为什么即使你有这么多单变量而不是矢量?

Make

B(bool t1, bool t2, bool t3.......................bool tn);

To

B(std::vector<bool>& booleanValues);

Pass booleanValues to A so you get this code:

将booleanValues传递给A,以便获得此代码:

B(std::vector<bool>& booleanValues)
{
     A(booleanValues);
}

#2


2  

You could create a constructor for class b that takes class a as a parameter, in the constructor you would do the assignment. Or you could overload the = operator. Ultimately you could do:

您可以为类b创建一个构造函数,它将类a作为参数,在构造函数中进行赋值。或者你可以重载=运算符。最终你可以这样做:

class b = class a;

if you take the route of overloading the = operator.

如果你采取重载=运算符的路线。

You can read about the copy constructors and operator = here: http://www.learncpp.com/cpp-tutorial/911-the-copy-constructor-and-overloading-the-assignment-operator/

您可以在这里阅读有关复制构造函数和operator =的信息:http://www.learncpp.com/cpp-tutorial/911-the-copy-constructor-and-overloading-the-assignment-operator/

A word of caution, make sure to read the article if you choose the = operator route because it takes some getting used to.

需要注意的是,如果您选择=运算符路由,请务必阅读该文章,因为它需要一些时间来习惯。

#3


1  

well, the real question is weather the classes are the same, have the same struct, have some parts that are the same or is one of them is kind of the other kind.

好吧,真正的问题是天气类是相同的,有相同的结构,有一些相同的部分或其中一个是另一种。

  • same class: use only 1 class and create as much instance as you need
  • 同一个类:只使用1个类,并根据需要创建尽可能多的实例

  • same struct: if they have the same struct but different methods you can make a parent class or leave them as they are, cus' same struct doesn't necessarily say same meaning.
  • 相同的结构:如果它们具有相同的结构但是具有不同的方法,则可以创建父类或保持它们原样,cus'相同的结构不一定表示相同的含义。

  • are the same or is one of them is kind of the other kind: this probably say inheritance. you'll need to decide based on logic which parts are supposed to go to the parent class and weather the parent class should be abstract, or i A parent of B, the other way around or a new parent class for both
  • 是相同的还是其中一种是另一种:这可能是继承。你需要根据逻辑决定哪些部分应该转到父类,并且父类的天气应该是抽象的,或者我是B的父类,反过来或两者的新父类

#4


1  

You can do copy constructor (and also conversion constructors) and operator= as mentioned in another answer, and put all the bools in an array as also mentioned in another answer. If you need or want the bools to have discrete variable names, you can put them in a struct and use that struct to pass the values. This code just passes the struct around with a Get() and Set(): If you do a copy constructor and operator= the struct isn't necessary since the large assignment code is hidden in member functions and not seen when passed from object to object. But it may be best to retain it if the different types of objects will always contain the same set of bools and the bools are all related in some way.

您可以复制构造函数(以及转换构造函数)和operator =,如另一个答案中所述,并将所有bool放入数组中,如另一个答案中所述。如果您需要或希望bool具有离散变量名称,您可以将它们放在结构中并使用该结构来传递值。这段代码只是通过Get()和Set()传递结构:如果你做一个复制构造函数和operator =没有必要的结构,因为大的赋值代码隐藏在成员函数中,而在从对象传递到目的。但如果不同类型的对象总是包含同一组bool并且bool都以某种方式相关,那么最好保留它。

#include <iostream>
using namespace std;

struct States {
  bool left;
  bool right;
  bool up;
  bool down;
};

class A {
  States states;
public: 
  A (bool left, bool right, bool up, bool down) : states {left,right,up,down} {}
  void Print(void) { 
    cout << states.left << ',' << states.right << ',' << states.up << ',' << states.down << endl;
  }
  void Set(const States &states) { this->states = states; }
  const States &Get(void) { return states; }
};

class B {
  States states;
public:
  B (bool left, bool right, bool up, bool down) : states {left,right,up,down} {}
  void Set(const States &states) { this->states = states; }
  const States &Get(void) { return states; }
  void Print(void) { 
    cout << states.left << ',' << states.right << ',' << states.up << ',' << states.down << endl;
  }
};

int main()
{
  A a(true,false,true,false);
  a.Print();
  B b(true,true,true,true);
  b.Print();
  b.Set( a.Get() );
  b.Print();
  B bb(false,false,false,false);
  a.Set( bb.Get() );
  a.Print();
}