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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:29:16+00:00 2026-05-26T10:29:16+00:00

I am developing a project in C++ under Ubuntu 11.10 using the latest version

  • 0

I am developing a project in C++ under Ubuntu 11.10 using the latest version of NetBeans. I’ll only post minimal parts of the code relevant to the problem. Let’s say I have the following code for a graph kind of problem:

typedef map<Node*, double, DereferenceCompare> Transitions;

class Node {
    int _nodeNumber;
    Transitions _transitions;
}

Each Node object holds a map of pointers to other Node objects. Now we have:

typedef set<Node*, DereferenceCompare> Nodes;

class Network {
    Nodes _network;
}

The problem: I am at a loss about writing a copy constructor for the class Network. What I am trying to achieve is to be able to do the following:

Network n1;
Network n2(n1);
//Have both n1 and n2 identical in structure but distinct in memory (deep copy).

Am I right in the following assumption: if I write a copy constructor for the Node class it would need to also copy the Transitions container. The Transitions container at that moment would hold pointers to the old nodes as the new ones do not exist yet.

This is my first post here. I hope I provided clear and sufficient information. I can further clarify if I was not coherent enough with my problem. Any help would be greatly appreciated.

  • 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-26T10:29:17+00:00Added an answer on May 26, 2026 at 10:29 am

    I’ve done this exact same thing before. It’s tricky:

    Network::Network(const Network& b) {
        //old to new mapping
        std::unordered_map<Node*, Node*> mapper(b._network.size()); 
        // duplicate all nodes without links
        for(auto iter = b.begin(); iter != b.end(); ++iter) {
            Node* new_node = new Node();
            try {
                _network.insert(new_node);
            } catch (std::bad_alloc& e) {
                delete new_node;
                throw;
            }
            mapper[iter->first] = _network; //and map the old Nodes to new ones
            new_node->_nodeNumber = iter->_node_number;
        }
        // THEN map the links
        for(auto iter = b.begin(); iter != b.end(); ++iter) {
            Node* new_node = mapper[iter->first];
            //for each link in the old one
            for(auto iter2 = iter->_transitions.begin(); 
                     iter2 != iter->_transitions.end();
                     ++iter2)
            {
                //link to the corresponding new node
                Node* connection = mapper[iter2->first];
                new_node->_transitions[connection ] = iter2->second;
            }
        }
    }
    

    [EDIT] Now exception safe
    Also note I haven’t attempted to validate the code in any way other than to compile. I just remember this is what I did years ago when I ran into the same problem.

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

Sidebar

Related Questions

I'm developing a project on OS X Lion that is under Git version control.
I'm using PyCharm to write the code of a Django project I'm developing. My
I am developing project using Spring , Struts2 & Hibernate . Now I want
I'm developing a project using the MVVM pattern in WPF. One of the key
I'm using Netbeans 7.0.1 in a PHP project. I've installed XAMPP 1.7 in Windows
I'm developing on a Ubuntu 8.04 machine using Eclipse Ganymede. I installed Tomcat 5.5
I am developing under Windows, and using Eclipse with CDT to develop C++ applications.
I'm developing a little side-project to display multiple wallpapers on multiple screens under GNOME
I am developing one project using LWUIT, Midlet mobile Application. when I press number
Encountering a weird problem here. I'm developing a game for my school project (non-commercial),

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.