M
ma740988
Consider:
# include <iostream>
# include <algorithm>
# include <vector>
# include <string>
using namespace std;
class msg {
std::string someStr;
public:
msg(const std::string& sStr)
: someStr(sStr) {}
msg( const msg& rhs )
: someStr(rhs.someStr)
{}
void swap( msg& rhs ) {
std::swap( someStr, rhs.someStr );
}
msg& operator=( const msg& rhs) {
msg tmp (rhs);
swap(tmp);
return *this;
}
std::string get_some_str() const { return someStr; }
};
class Y {
typedef std::vector<msg> msgVec;
msgVec msg_vec;
public:
Y(){}
Y( const Y& rhs )
: msg_vec(rhs.msg_vec)
{}
void swap( Y& rhs ) {
std::swap( msg_vec, rhs.msg_vec );
}
Y& operator=( const Y& rhs) {
Y tmp (rhs);
swap(tmp);
return *this;
}
void add(msg& ms)
{ msg_vec.push_back(ms); }
void print()
{
if (msg_vec.size())
std::cout << msg_vec[0].get_some_str() << std::endl; // just for
test
}
};
class Controller {
typedef std::vector<Y> VEC_Y;
VEC_Y vectorOfYs;
public:
Controller() {}
Controller( const Controller& rhs )
: vectorOfYs(rhs.vectorOfYs)
{}
void swap( Controller& rhs ) {
std::swap( vectorOfYs, rhs.vectorOfYs );
}
Controller& operator=( const Controller& rhs) {
Controller tmp (rhs);
swap(tmp);
return *this;
}
void add_y(Y myY)
{ vectorOfYs.push_back(myY);}
void add_msg ( msg& ms )
{
if (vectorOfYs.size())
vectorOfYs[0].add(ms); // brute force for test.
}
void print() {
if (vectorOfYs.size())
vectorOfYs[0].print(); // just experimenting
}
};
int main()
{
Controller ctrl1;
ctrl1.add_y(Y());
ctrl1.add_msg(msg("test"));
Controller ctrl2;
ctrl2 = ctrl1;
ctrl2.print();
}
For assignment between two controller objects, the fact that the class
Controller contains a vector of Y's which in turn contains a vector of
msg's; implies that theres - perhaps a requirement for a copy
constructor in all _three_ classes. Correct?
# include <iostream>
# include <algorithm>
# include <vector>
# include <string>
using namespace std;
class msg {
std::string someStr;
public:
msg(const std::string& sStr)
: someStr(sStr) {}
msg( const msg& rhs )
: someStr(rhs.someStr)
{}
void swap( msg& rhs ) {
std::swap( someStr, rhs.someStr );
}
msg& operator=( const msg& rhs) {
msg tmp (rhs);
swap(tmp);
return *this;
}
std::string get_some_str() const { return someStr; }
};
class Y {
typedef std::vector<msg> msgVec;
msgVec msg_vec;
public:
Y(){}
Y( const Y& rhs )
: msg_vec(rhs.msg_vec)
{}
void swap( Y& rhs ) {
std::swap( msg_vec, rhs.msg_vec );
}
Y& operator=( const Y& rhs) {
Y tmp (rhs);
swap(tmp);
return *this;
}
void add(msg& ms)
{ msg_vec.push_back(ms); }
void print()
{
if (msg_vec.size())
std::cout << msg_vec[0].get_some_str() << std::endl; // just for
test
}
};
class Controller {
typedef std::vector<Y> VEC_Y;
VEC_Y vectorOfYs;
public:
Controller() {}
Controller( const Controller& rhs )
: vectorOfYs(rhs.vectorOfYs)
{}
void swap( Controller& rhs ) {
std::swap( vectorOfYs, rhs.vectorOfYs );
}
Controller& operator=( const Controller& rhs) {
Controller tmp (rhs);
swap(tmp);
return *this;
}
void add_y(Y myY)
{ vectorOfYs.push_back(myY);}
void add_msg ( msg& ms )
{
if (vectorOfYs.size())
vectorOfYs[0].add(ms); // brute force for test.
}
void print() {
if (vectorOfYs.size())
vectorOfYs[0].print(); // just experimenting
}
};
int main()
{
Controller ctrl1;
ctrl1.add_y(Y());
ctrl1.add_msg(msg("test"));
Controller ctrl2;
ctrl2 = ctrl1;
ctrl2.print();
}
For assignment between two controller objects, the fact that the class
Controller contains a vector of Y's which in turn contains a vector of
msg's; implies that theres - perhaps a requirement for a copy
constructor in all _three_ classes. Correct?