Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6094689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:42:32+00:00 2026-05-23T12:42:32+00:00

I am puzzled because I cannot figure where my bug/problem is. I have a

  • 0

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, but 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 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:42:33+00:00Added an answer on May 23, 2026 at 12:42 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an MFC dialog in which there are two radio buttons. I have
I have been really puzzled because I was trying to work out some jQuery
Recently I have started learning python on windows. I am puzzled because the way
This one has me puzzled. It seemed like an easy task, but the solution
I'm a little puzzled with QueryStrings and ActionResult I have a URL coming in
Here's another one I've been thinking about lately. We have concluded in earlier discussions
I am just puzzled here. Maybe because it's the end of the day, I
I'm puzzled because of build and run errors that mislead me. From them, I
We have switched from sqlite to FireBird embedded server, because FB seems to support
I am puzzled that there are two entries to enter for the code sign

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.