user2713375
user2713375

Reputation: 63

C++ Copy values of one object into another object

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 .

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

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

Upvotes: 3

Views: 8012

Answers (4)

Scooter
Scooter

Reputation: 7059

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.

#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();
}

Upvotes: 1

No Idea For Name
No Idea For Name

Reputation: 11597

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
  • 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.
  • 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

Upvotes: 1

Ed Smilovici
Ed Smilovici

Reputation: 304

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:

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/

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

Upvotes: 3

stupidstudent
stupidstudent

Reputation: 698

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?

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:

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

Upvotes: 2

Related Questions