Question : How to write my deep copy constroctur?

Hi experts,

I am trying to write a copy constructor for my class but I am a bit stuck. The class is container class MyContainer which has a map as a private member. The map is made up off objects that is inheriting from the class MyElement. There is two classes that inherit from MyElement Element1 and Element2.

class MyContainer
{
..
..
private:
   map<string, MyElement*> *mpContainer;
};

MyContainer::MyContainer(const MyContainer &rMyContainer)
{
    map<string, MyElement*>::const_iterator itr = rMyContainer.mpContainer->begin();
    for ( ; itr != rMyContainer.mpContainer->end(); ++itr ) {
        string key = (*itr).first;
        MyElement *pTmp = (*itr).second;
        MyElement *pNewElement = new ???(*pTmp);
        mpSnapshots->insert(make_pair(key, pNewElement));
    }
}

My problem is that I dont understand how I should know which copy constructor I should call for the MyElement since I have two classes that inherit from it Element1 and Element2. Both of which could be part of the mpContainer map that is a member of the MyContainer class. Do I have to use some kind of instance of or what? Maybe it's late but I cannot figure it out.

Thanks experts

Answer : How to write my deep copy constroctur?

(a.)  Just means that you don't have to put static in front of an inner interface.  It can always be referenced, while an inner class will produce compile-time errors if it's used outside of the classes unless it's static.  See first code snippet attached.

(b.) This seems to be false, as I can do the following without any errors:

class MyClass1 {
      public static class MyInnerClass1 {
            public static int a = 0;

            public static void method() {
            }
      }
}

public class WhiteMage {
      public static void main(String[] args) {
            MyClass1.MyInnerClass1.a = 45;
            MyClass1.MyInnerClass1.method();
      }
}
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
class MyClass1 {
	public static interface MyInnerInterface1 {
	}

	public interface MyInnerInterface2 {
	}

	public static class MyInnerClass1 {
	}

	public class MyInnerClass2 {
	}
}

//no errors
class MyClass2 implements MyClass1.MyInnerInterface1,MyClass1.MyInnerInterface2 {
}

/**
 *
 * @author WhiteMage at http://www.experts-exchange.com/
 */
public class WhiteMage {
	public static void main(String[] args) {
		MyClass1.MyInnerInterface1 a = new MyClass2(); //no error
		MyClass1.MyInnerInterface2 b = new MyClass2(); //no error
		MyClass1.MyInnerClass1     c = new MyClass1.MyInnerClass1(); //no error

		//ERROR!
		MyClass1.MyInnerClass2     d = new MyClass1.MyInnerClass2();
	}
}
Random Solutions  
 
programming4us programming4us