1

Consider this, please:

class MyTreeNode: TreeNode{
   int x;
   public MyTreeNode(TreeNode tn)
   {
      x=1; 
      // Now what to do here with 'tn'???
   }

I know how to use x. But how should I use tn here to assign it to my MyTreeNode object?

dghadagh
  • 103
  • 1
  • 15
  • In your code there is no TreeNode object in MyTreeNode. You just set the class MyTreeNode to inherit of TreeNode and provide it's public methods/Props and use private methods. This you can access with base.x. If you want to store a TreeNode object in MyTreeNode u need to instaciated one – Yosh Synergi Nov 25 '15 at 08:30
  • 1
    I guess you have an instance of `TreeNode`, and you want to build a `MyTreeNode` that is derived from `TreeNode`, with all same values in it, and an extra property `x`, am I correct? – Cheng Chen Nov 25 '15 at 08:30
  • You are exactly right Danny Chen. Do you know any solution? – dghadagh Nov 25 '15 at 08:40

2 Answers2

2

Why do you want to assign the tn to your MyTreeNode? It allready inherits from it. If you´re planning to create a copy of tn but of type MyTreeNode you should create a copy-constructor:

int x;
public MyTreeNode(TreeNode tn)
{
    // copy tn´s attributes first
    this.myProp = tn.myProp;
    // ... all the other properties from tn

    // now set the value for x
    this.x = 1; 
}

However if you also have private members on your base-class which have to be copied this is much more difficult, you´d have to use reflection in this case to have access to those private members (e.g. fields).

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
2

As other comments stated you need a copy constructor. I would use the following code, so that I can also copy private properties without reflection.

class TreeNode
{
    private int myProp; //value type field
    private TreeNode parentNode; //reference type field
    public TreeNode(TreeNode tn) //copy constructor
    {
        //copy all the properties/fields that are value types
        this.myProp = tn.myProp;
        //if you have reference types fields properties you need to create a copy of that instance to it as well
        this.parentNode = new TreeNode(parentNode);
    }
    //You can have other constructors here
}

class MyTreeNode: TreeNode{
   int x;
   public MyTreeNode(TreeNode tn):base(tn) //This calls the copy constructor before assigning x = 1
   {
      x=1; 
   }
Beniamin E.
  • 156
  • 7
  • 1
    Oh, the idea of the copy-ctor within the base-class to avoid reflection is really nice. +1 for this – MakePeaceGreatAgain Nov 25 '15 at 08:40
  • But TreeNode is a .NET base class and I cannot modify it to add a copy constructor for it. Can I? – dghadagh Nov 25 '15 at 08:59
  • 1
    In this case you need to create the constructor in the derived class, as @HimBromBeere showed you. Here http://stackoverflow.com/questions/8631898/c-sharp-inheritance-derived-class-from-base-class[/link] It is shown how you cand do it with reflection. – Beniamin E. Nov 25 '15 at 09:34