开发者

C++ vectors, sorting and custom class operators

I am puzzled because I cannot figure where my bug/problem is. I have a class Instruction, which uses two custom operators, one assignment and one comparison operator. Previously I only used the comparison operator in order to use std::sort to sort Instructions based on one of their members, an std::string name. However, since I started re-factoring the entire project, I changed some members to constant. This lead me to having to use an initialization list for those constants. This in turn, lead me to have to create an assignment operator, because those instructions are being copied when pushed back in vectors. This is where everything goes wrong. I include my Class declaration and Constructor and Operators.

instruction.hpp

class Instruction 
{
  private:  
    unsigned int param_size;
    const float max_angle, min_angle;
    bool micro_mutated;
  protected:
    const std::string body_part;
    std::vector<Parameter <float> > parameters;
  public:
    Instruction(std::string name, float max, float min);
    Instruction operator=(const Instruction& I);
    bool operator<(const Instruction& I) const;
    //there are a few more functions but are completely irrelevant
}

instruction.cpp:

Instruction::Instruction(std::string name,float max, float min) :
body_part (name), max_angle(max), min_angle(min)
{}

Instruction Instruction::operator=(const Instruction& I)
{
  (*this) = I;
  return (*this);
}

bool Instruction::operator<(const Instruction& I) const
{
  return body_part < I.body_part;
}

The only reason why I created an assignment operator (which to be honest I've never done before) was because when I was trying to push_back Instructions, compiler complained about not being able to instantiate "from here" Instructions and I thought it had to do with the constant members. Without the members being constant, everything worked fine, even the sorting. Now the weird part. If I remove the std::sort, the above code works, bu开发者_运维技巧t not all the time. Some times it crashed after a while, some times it won't crash. But the moment I include the sorting, it crashes straight away. Can someone please help ?


Don't forget the rule of threes: if you have one of a copy construct, copy assignment operator, and destructor, then you should have all of them.

However, your copy assignment operator is effectively an infinite loop; it calls itself. Operator= will be used whenever you have anything of the form: Instruction &=Instruction&. Which is exactly what (*this) = I is.

My question is this: why are those things constant? Having members be constant effectively means that you cannot copy the object (unless you use a const-cast) with copy assignment. You can copy construct them, but that's about it.

Is there some reason for those members to be constant? And if so, then you shouldn't be copying these objects by assignment. The two are mutually exclusive.

If you need a member to be effectively constant, unchangable by outside activity (but not language-const which prevents copying), then this should be done with proper accessor methods. Provide users of the class with ways to get these values, but not ways to set them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜