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

  • Home
  • SEARCH
  • 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 8603057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:14:05+00:00 2026-06-12T02:14:05+00:00

For customized use, I have inherited std::vector to a custom class Vector . For

  • 0

For customized use, I have inherited std::vector to a custom class Vector. For my requirement this public inheritance is ok.

One intention is to avoid making copies of a vector array multiple times, so I decided this custom class to be ‘ownership’ based.

Vector<A> vA1(100); // vA1 is allocated A[100]
Vector<A> vA2 = vA1; // vA2 refers to original A[100] and ...
                     // ... vA1 is now blank which is expected

This is how it’s implemented for C++03:

template<typename T>
struct Vector : std::vector<T>
{
  // ... other constructors

  Vector (const Vector &copy) // copy constructor
  {
    this->swap(const_cast<Vector&>(copy)); // <---- line of interest
  }
  // 'operator =' is TBD (either same as above or unimplemented)
};

Am I breaking any language rule/feature/convention ? Is there anything bad in this code ?

Edit: I have added my new approach in form of answer below (Here is its working demo).

  • 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-06-12T02:14:06+00:00Added an answer on June 12, 2026 at 2:14 am

    With the insight from comments/answers, I realized that my current approach is not a good idea to deal with the things.
    I have come up with a changed designed where I try to simulate the “move” operation using swap() method (consider them interchangeable in this context).

    For any class or container, which wants to make faster copying (i.e. move by swapping) should inherit below class (directly copy pasting from the real file):

      /*
       * An empty CRTP base class for the classes which are allowing move sematics (effectively swap)
       * A given class `X` must implement `void swap(X&);` method for the movement (i.e. content swapping)
       * For example, `X` is deriving this class (i.e. `class X : public Movable<X> ...`)
       * ... below is an illustration for how to transfer contents of an `X` object to another
       * `X o1; ... X o2(o1.move()); // contents of o1 is now moved to o2`
       */
      template<typename Derived>
      class Movable
      {
        /*
         * Empty constructor for normal behavior
         */
        public: Movable ()
                {}
    
        /*
         * Copy constructor which does nothing; without this compiler errors out !
         */
        public: Movable (const Movable &copy)
                {}
    
        /*
         * Move constructor which will effectively call `Derived::swap()` method
         * After this, the contents of the object will be moved to each other
         * For syntactic sugar, it has been made `explicit`
         */
        public: explicit Movable (Movable &orig)
                {
                  (*this)(orig);
                }
    
        /*
         * Moving while Not initializing object, one need to use `operator ()` and not `operator =`
         * If `operator =` is used then simply `move()` part will not happen; i.e. below 2 are same:
         * `obj1 = obj2.move();` and `obj1 = obj2;`
         */
        public: void operator () (Movable &orig)
                {
                  static_cast<Derived*>(this)->swap(static_cast<Derived&>(orig));
                }
        /*
         * This method is called from `Derived` class when move sematics is intended
         */
        public: Movable& move ()
                {
                  return *this;
                }
      };
    

    Here how it’s supposed to be deployed:

    template<typename T>
    struct Vector : std::vector<T>,
                    Movable<Vector<T> >  // inherit as CRTP base
    {
      // ... other methods
      typedef Movable<Vector<T> > Movable_;
      Vector (Movable_ &orig) : Movable_(orig) {}  // Declare a constructor
    };
    

    Here how it’s supposed to be used:

    Vector<A> vA1(100); // vA1 is allocated A[100]
    Vector<A> vA2 = vA1; // normal copying (no change)
    vA2 = vA1; // normal assignment (no change)
    Vector<A> vA3(vA1.move()); // <------- "moves" contents from 'vA1' to 'vA3'
    vA1(vA2.move()); // <------- "moves" contents from 'vA2' to 'vA1'
    vA2 = vA3.move(); // normal copying from 'vA3' to 'vA2' ('vA3' unaffected)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this menu that I customized to use it like a select. It
I have panel that I have customized. I use it to display text. But
I have created a custom button called ModuleUIButton which inherits from UIButton . This
i have the following customized security controller using the secure module for play: public
I have implemented a custom designer class which inherits from DocumentDesigner. The standard Form
I have a List of custom object, which consist of a custom list. class
Given an html/javascript 'widget' which needs to have certain fields customized before use. For
I have a customized .profile that I use in ksh and below is a
While I use Linux, I have not yet customized my bash shell, which I
I have a customized UIPickerview and I do not want to use a datepicker.

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.