Reputation: 63
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
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
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.
Upvotes: 1
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
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