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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:17:13+00:00 2026-05-25T14:17:13+00:00

Problem is to create a std::vector data table ( struct MegaTable , from the

  • 0

Problem is to create a std::vector data table (struct MegaTable, from the example below), where items (struct DataItem, from the example below) could have a pointer to the whole data array.

Here is my code:

#include <vector>

struct MegaDataItem;

struct DataItem 
{
     int            a_;
     char*          ch_;

     MegaDataItem*  ptr_;

     DataItem( int _a, char* _ch, MegaDataItem* ptr )
          : a_( _a )
          , ch_( _ch )
          , ptr_( ptr )
     {}
};
typedef std::vector< DataItem > DataTable;


struct MegaDataItem 
{
     int            b_;
     DataTable      data_;

     MegaDataItem( int _b )
          : b_( _b )
     {
          for ( int j = 15; j >= 10; j-- )
          {
               DataItem item( j, "", this );
               data_.push_back( item );
          }
     }
};
typedef std::vector< MegaDataItem > MegaTable;


int main( int argc, char** argv )
{
     MegaTable table;

     for ( int i = 0; i < 5; i++ )
     {
          MegaDataItem megaItem( i );
          table.push_back( megaItem );
     }
     return 0;
}

And debug snapshot from MSVS:

enter image description here

As you can see, ptr_ pointer everywhere equals to 0x0031fccc, but that is not correct! Pointer must consists with correct struct MegaDataItem data, where all struct DataItem‘s exists…

Thanks for the help!

PS. I know that this is not the hard question, but I can’t get it, how to get this things work!


UPDATE (corrected solution):
PS: thnks to jpalecek! 🙂

MegaDataItem( const MegaDataItem& other )
          : b_( other.b_ )
     {
          data_.clear();
          DataTable::const_iterator d_i( other.data_.begin() ), d_e( other.data_.end() );
          for ( ; d_i != d_e; ++d_i )
               data_.push_back( DataItem( (*d_i).a_, (*d_i).ch_, this ) );
     }

void operator=( const MegaDataItem& other )
     {
          b_ = other.b_;

          data_.clear();
          DataTable::const_iterator d_i( other.data_.begin() ), d_e( other.data_.end() );
          for ( ; d_i != d_e; ++d_i )
               data_.push_back( DataItem( (*d_i).a_, (*d_i).ch_, 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-25T14:17:14+00:00Added an answer on May 25, 2026 at 2:17 pm

    The problem is your MegaDataItem structure is not copyable and assignable (if you copy or assign the vector in MegaDataItem, the back pointers will point to the original MegaDataItem, which is incorrect). You have to amend that.

    Particularly, you’ll have to implement the copy-constructor and assignment operator. In these, you have to redirect the ptr_ pointers in DataItems to the new MegaDataItem.

    Example implementation:

    MegaDataItem(const MegaDataItem& other) : b_(other.b_) {
      // fill data
      for(DataItem& item : other.data_)
        data_.push_back(DataItem(item.a_, item.ch_, this));
    }
    
    operator=(const MegaDataItem& other) {
      b_=other.b_;
      data_.clear();
      for(DataItem& item : other.data_)
        data_.push_back(DataItem(item.a_, item.ch_, this));
    }
    

    BTW, depending on what ch_ in DataItem means, you may want to implement these in DataItem, too.

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

Sidebar

Related Questions

i have very simple problem. I need to create model, that represent element of
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
I have a std::vector<uint8_t> that contains strings at specific offsets. Here's a shortened dump:
So I have a vector of pointers holding a baseclass. I create two elements
i'm having a problem to create a text_field without a method association. Maybe i
Having a problem trying to create a function, as part of a BizTalk helper
Visual Studio 2008 w/Sp1 To reproduce my problem I simply create a new .Net
Problem I've got a collection of IThing s and I'd like to create a
I create new ASP.NET web application that use SMTP to send message. The problem
I'm having a problem using the java.text.MessageFormat object. I'm trying to create SQL insert

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.