N
Nobody
The requirement that STL container elements have to be assignable is causing
me a problem.
Consider a class X which contains both const and non-const data members:
class X
{
public:
X(const std::string &rStrId, int x) : m_strId(strId), m_x(x)
{
}
private:
X& operator=(const X &rhs); // not allowed
X(const X& rhs); // not allowed
const std::string m_strId;
int m_x;
};
I know that because I have the data member, m_strId, declared as *const*, it
means that I cannot assign to it. This was OK as I didn't need to and
therefore made it private with no implementation.
Then I decided that I needed to have an STL vector of Xs.
X x(std::string("hello"), 43);
std::vector<X > xVector;
....
xVector.push_back(X(std::string("hello"), 43));
I know too that when using STL containers the standard says that the
contained objects must be copyable and assignable.
This is my dilemma. If I don't supply an assignment operator for X or I make
it private then it won't compile when I try to push an instance of X into
the vector.
I have considered the following options:
1) Make the string non const and let the compiler generate the assignment
operator. I don't want to do this as each instance of X is required to have
a unique string identifier, m_strId, which will not change throughout the
lifetime of the object.
2) Provide an assignment operator that only assigns the non const int m_x.
This would allow the possibility of assigning one X object to another and
the result not being equal.
Which is the best option of the two? Are there any other alternatives I
haven't considered which are better?
TIA
RP
me a problem.
Consider a class X which contains both const and non-const data members:
class X
{
public:
X(const std::string &rStrId, int x) : m_strId(strId), m_x(x)
{
}
private:
X& operator=(const X &rhs); // not allowed
X(const X& rhs); // not allowed
const std::string m_strId;
int m_x;
};
I know that because I have the data member, m_strId, declared as *const*, it
means that I cannot assign to it. This was OK as I didn't need to and
therefore made it private with no implementation.
Then I decided that I needed to have an STL vector of Xs.
X x(std::string("hello"), 43);
std::vector<X > xVector;
....
xVector.push_back(X(std::string("hello"), 43));
I know too that when using STL containers the standard says that the
contained objects must be copyable and assignable.
This is my dilemma. If I don't supply an assignment operator for X or I make
it private then it won't compile when I try to push an instance of X into
the vector.
I have considered the following options:
1) Make the string non const and let the compiler generate the assignment
operator. I don't want to do this as each instance of X is required to have
a unique string identifier, m_strId, which will not change throughout the
lifetime of the object.
2) Provide an assignment operator that only assigns the non const int m_x.
This would allow the possibility of assigning one X object to another and
the result not being equal.
Which is the best option of the two? Are there any other alternatives I
haven't considered which are better?
TIA
RP