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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:38:04+00:00 2026-05-22T02:38:04+00:00

I’m trying to write a copy constructor for an object managing a STL map

  • 0

I’m trying to write a copy constructor for an object managing a STL map containing pointers, where the key is a string. However, when I attempt to insert new values in the map, the pointers are set to NULL:

// ...
for(std::map<std::string, data_base*, order>::const_iterator it = other.elements.begin();
it != other.elements.end(); ++it){
    data_base *t = it->second->clone();
    std::cout << "CLONE: " << std::hex << t << std::endl;
    elements[it->first] = t;
    std::cout << "INSERTED: " << std::hex << elements[it->first] << std::endl;
}
// ...

other is the object being copied and elements the map. The clone() method returns a pointer to a new object (via new).

Running the code above I get something like:

CLONE: 0xcfbbc0
INSERTED: 0

I’m not a very experienced programmer and this issue is probably simple to fix, but I didnt find any solution to it searching around.

Thanks a lot for your time.

  • 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-22T02:38:05+00:00Added an answer on May 22, 2026 at 2:38 am

    I don’t see any problem with this code, other than maybe

    std::map<std::string, data_base*, order>::const_iterator it
    

    Here order gives the key comparator to use to sort the pairs contained in the map (often implemented as a tree).

    Maybe you’re doing something wrong in it, making your [] operator don’t find the right ke, making your last line logging a new pair with a null ptr.

    First, try without that order, using the default key-comparator (std::less), then if it don’t work, post your order definition and the map declaration. If it’s not enough, just provide a simple complete program that reproduce the problem.


    I just wrote a simple similar test, using the default key-comparator :

    #include <map>
    #include <string>
    #include <iostream>
    
    struct Data 
    { 
        int k; 
    
        Data* clone() { return new Data(); }
    };
    
    typedef std::map< std::string, Data* > DataMap;
    
    DataMap data_map;
    
    
    int main()
    {
        data_map[ "hello" ] = new Data();
        data_map[ "world" ] = new Data();
    
        DataMap other_map;
    
        for( DataMap::const_iterator it = data_map.begin(); it != data_map.end(); ++it)
        {
                Data*t = it->second->clone();
                std::cout << "CLONE: " << std::hex << t << std::endl;
                other_map[it->first] = t;
                std::cout << "INSERTED: " << std::hex << other_map[it->first] << std::endl;
        }
    
        std::cin.ignore();
    
        return 0;
    }
    

    On VS2010SP1, this outputs :

    CLONE: 00034DD0
    INSERTED: 00034DD0
    CLONE: 00035098
    INSERTED: 00035098
    

    So it should be the problem, or maybe you’re doing something wrong before.

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

Sidebar

Related Questions

i got an object with contents of html markup in it, for example: string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And
I am trying to loop through a bunch of documents I have to put

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.