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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:25:38+00:00 2026-05-28T08:25:38+00:00

I’m using this two classes // This is generic data structure containing some binary

  • 0

I’m using this two classes

// This is generic data structure containing some binary data
class A {
public:
    A();
    A(const A&);
    ~A();
}

// Main data container
class B {
public:
    B();
    B( const B&);
    ~B();
protected:
    std::vector<A *> data;
}

// Copy constructor for class b
B::B( const B& orig):data() {
    for( std::vector<A *>::const_iterator it = orig.data.begin();
        it < orig.data.end(); ++it){
        data.push_back( new A( *(*it)));
    }
}

I guess this class would do its job, but I’m looking for a way to reach total perfection.

First, :data() – is this initialization required to initialize empty vector correctly (is it clean code)?

How is vector::iterator used in copy constructor? The only way I found is the one I’ve written into code (const should be mandatory for copy constructor).

Does copying the vector copy pointer values and not whole objects?

And finally new data initialization… Is there any way to replace the whole loop with less code and/or is there any standard how to write copy constructor for std::containers which contain object pointers?

Sub question: I’m assuming using vector<A *> is much more suitable and effective for various reasons than just vector<A> (not copying every time, power to decide whether (not) to copy objects…) Is this assumption correct?

  • 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-28T08:25:39+00:00Added an answer on May 28, 2026 at 8:25 am

    The data() is not necessary because that will be done automatically to the vector before the constructor is entered. You only need to initialise members that are POD (plain old data) types or types which have no default constructor (or references, constants, etc).

    You can initialise the vector with the number of elements that the other one has, so that the vector doesn’t have to resize itself as it grows. If you don’t, you’re starting with a small vector and making it incrementally reach the destination size via allocations and reallocations. This will make the vector the correct size from the very beginning:

    B::B(const B& orig) : data(orig.data.size()) {
        for (std::size_t i = 0; i < orig.data.size(); ++i)
            data[i] = new A(*orig.data[i]);
    }
    

    Notice that you are not using push_back any more because the vector is already full of orig.data.size() number of elements that are default constructed (which is NULL in the case of pointers).

    This also trims down the code because you can use an integer to iterate it instead of an iterator.

    If you really want to use iterators, you can do

    B::B(const B& orig) : data(orig.data.size()) {
        // auto is preferable here but I don't know if your compiler supports it
        vector<A*>::iterator thisit = data.begin();
        vector<A*>::const_iterator thatit = orig.data.cbegin();
    
        for (; thatit != orig.data.cend(); ++thisit, ++thatit)
            *thisit = new A(**thatit);
    }
    

    The advantage of this is that it will work with other container types (like list) by just changing the types of the iterators (but of course that would go away if you have auto).

    If you want to add exception-safety, you need a try/catch block:

    B::B(const B& orig) : data(orig.data.size()) {
        try {
            // auto is preferable here but I don't know if your compiler supports it
            vector<A*>::iterator thisit = data.begin();
            vector<A*>::const_iterator thatit = orig.data.cbegin();
    
            for (; thatit != orig.data.cend(); ++thisit, ++thatit)
                *thisit = new A(**thatit);
        } catch (...) {
            for (vector<A*>::iterator i = data.begin(); i != data.end(); ++i)
                if (!*i)
                    break;
                else
                    delete *i;
    
            throw;
        }
    }
    

    This way you will not have a memory leak if one of the new calls throws an exception. Of course you can use the try/catch along with the way without iterators if you’d rather do it that way.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I am reading a book about Javascript and jQuery and using one of the
this is what i have right now Drawing an RSS feed into the php,

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.