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 7186097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:33:37+00:00 2026-05-28T18:33:37+00:00

I have a class Step derived from std::vector<unsigned int> . I need to overload

  • 0

I have a class Step derived from std::vector<unsigned int>. I need to overload assignment operator because of the deep copy used in assignment of a value returned from a static method. I can’t figur out how should I copy all elements of rhv to this in assignment:

class Step : public std::vector<unsigned int>
{
public:
    friend std::ostream& operator<<(std::ostream& outStream, const Step& step);
    Step& operator =(const Step& rhv);
    static Step fromString(const std::string &input);
    // Something like: Step x = Step::fromString("12 13 14 15 16");
private:
    double time;
    double pause;
    unsigned int id;
    std::string name;
};

and then overloading =:

Step& Step::operator =(const Step& rhv)
{
    time = rhv.time;
    pause = rhv.pause;
    id = rhv.id;
    // How should I copy contents of rhv to `this` safely?
    return *this;
}
  • 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-28T18:33:38+00:00Added an answer on May 28, 2026 at 6:33 pm

    I’m not 100% sure from your question, but I think you are asking about calling the parent operator=. In that case you have two options:

    std::vector<unsigned int>::operator=(rhv); //either explicitly call the parent assignment op
    *static_cast<std::vector<unsigned int>*>(this) = rhv; //or cast this to parentclass and call assignment on that
    

    Of course in the code you have shown us you don’t do any manual resource handling, so I don’t see why you want to write your own assignment operator, the compiler generated one should do fine. Besides if you write your own assignment operator you might want to head the rule of three and write your own copy constructor and destructor too (at least in C++03, C++11 can be a bit different due to movable but not copyable classes).

    As another sidenote: Most standardlibrary classes are not designed to be derived from, so you might want to rethink your design requiering you to inherit form std::vector

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have class A: public class ClassA<T> Class B derives from A: public class
I have class A: public B { ...} vector<A*> v; I want to do
I have class Fred { public: void inspect() const {}; void modify(){}; }; int
I have the following class structure: class TestCase { public IList<Step> Steps { get;
I have to implement ISerializable in a derived class (to do some custom serialization/deserialization)
I have an abstract class Step, and many descendant Step classes that I would
i have a Class : public class Company { public int id { get;
I have class method that returns a list of employees that I can iterate
I have: class MyClass extends MyClass2 implements Serializable { //... } In MyClass2 is
I have class with internal property: internal virtual StateEnum EnrolmentState { get { ..getter

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.