1

I am confused about the object creation concerning the data members of the BSTNode class. For example, in the header file one of the data members is defined as "Key k". Does this mean that the default key has already been created and I don't need to write anything in the default BSTNode constructor or do I still need to write Key k; in the constructor to create the default key? Does this still hold true when I am passing a Key to set as k in the constructor?

Class definition (in header):

 class BSTNode {
 public:
   BSTNode();
   BSTNode(Key k, Value v);

   Key k;
   Value v;
   BSTNode* left;
   BSTNode* right;
};

This is my attempt:

template <class Key, class Value>
BSTNode<Key,Value>::BSTNode(){
    Key kk; //create th default key
    k = kk; //set the BSTNode's k to the default key
    Value vv; //create the default value
    v = vv; //set the BSTNode's v to the default value
    BSTNode* left = NULL; //set left pointer to NULL
    BSTNode* right = NULL; //set right pointer to NULL
 }
Xerunix
  • 431
  • 1
  • 6
  • 21
  • Likely `Key` is an actual concrete class in the non-templated version. In the templated version, `Key` is a placeholder name for a type coming in. Usually people use the generic `T` or `U` to avoid name clashes with concrete types. – AndyG Apr 21 '15 at 15:07
  • 2
    Your constructor isn't doing anything. Better read some [C++ books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) first. – juanchopanza Apr 21 '15 at 15:08
  • @Andyg When I wrote the concrete implementation of the BST the key was just an int – Xerunix Apr 21 '15 at 15:11
  • Do you really need default constructor ? – Jarod42 Apr 21 '15 at 15:11
  • 1
    Most (if not all) of your problems are not related to templates. Maybe you should start with a bit more basic examples before turning to templates. – 463035818_is_not_an_ai Apr 21 '15 at 15:11
  • In this instance yes because it is for class. And understanding it will help me write the other constructor – Xerunix Apr 21 '15 at 15:12
  • @tobi303 I already wrote the entire BST without templates – Xerunix Apr 21 '15 at 15:12
  • 1
    To my knowledge, the line `BSTNode* left = NULL; //set left pointer to NULL` has no observable effect at all. Even if it was `BSTNode* left = new BSTNode();` it would be just a local variable hiding the member variable. And this is true whether its a template or not. – 463035818_is_not_an_ai Apr 21 '15 at 15:14
  • Also you constructor (code snippet 2) does not do anything (as mentioned already before). And the reason for this is not that its a template... – 463035818_is_not_an_ai Apr 21 '15 at 15:16
  • The constructor does not do *anything* to initialise the class's members, it just declares a bunch of local variables with the same names as the members. You should follow juanchopanza's advice and grab a book (or follow a good tutorial). – Angew is no longer proud of SO Apr 21 '15 at 15:17
  • @tobi303 what about code snippet 3? Is that the best way to make a key object and then set k to it? or can you do k = Key k; – Xerunix Apr 21 '15 at 15:17
  • `best` is rather subjective, but i would say it is rather `bad` to create temporaries when there is no need to do so. – 463035818_is_not_an_ai Apr 21 '15 at 15:19
  • How would you make a key object to set k to without creating a temporary one first? – Xerunix Apr 21 '15 at 15:20
  • @Xerunix Do you know *why* your code is wrong? I am not convinced that you actually know what others have been trying to tell you. – PaulMcKenzie Apr 21 '15 at 15:21
  • @PaulMcKenzie Of course I do. Key k; just creates a k named k and doesn't set the actual node's member variable to it. I fixed that when I wrote Key kk;\n k = kk to set the member variable to the key I created, but apparently that is a bad way to do it. What is a better way. k = new Key would return a Key* I believe and I don't want that. I don't really understand why setting BSTNode* left/right to NULL is bad though. – Xerunix Apr 21 '15 at 15:23
  • @Xerunix It isn't just a bad way, it is totally wrong. Also, it doesn't hurt to actually give your variables meaningful names. A name like `k` doesn't give meaning to what that variable denotes. – PaulMcKenzie Apr 21 '15 at 15:25
  • @PaulMcKenzie my professor came up with the name k for key which I am not allowed to change. It's also used by every teacher by in the entire department for dictionary ADT implementations so it can't be that bad. How is it totally wrong to create the default Key and then set k to it? – Xerunix Apr 21 '15 at 15:27
  • @Xerunix `It's also used by every teacher by in the entire department for dictionary ADT implementations so it can't be that bad.` When you actually write programs in the real world, tell your colleagues reading your code this information. They will either be angry, have a laugh, or both. There is tons of information as to why naming variables as one letter is bad -- your teachers are not doing you a favor. – PaulMcKenzie Apr 21 '15 at 15:34
  • @PaulMcKenzie I would never use a one letter variable on my own accord. Thanks for the actual answer. – Xerunix Apr 21 '15 at 15:38

1 Answers1

4

Your constructor is basically a no-op.

Let's go through each line:

template <class Key, class Value>
BSTNode<Key,Value>::BSTNode(){
    Key k;     // <--- local variable, destroyed on return
    Value v;   // <--- local variable, destroyed on return
    BSTNode* left = NULL;  // <--- local variable initialized to NULL, destroyed on return
    BSTNode* right = NULL; // <--- local variable initialized to NULL, destroyed on return
}

You are not initializing class member variables -- what you're doing is creating brand new, local variables, and setting them to a value. Once the constructor completes, those local variables no longer exist.

What you want to do is this:

class BSTNode {
 public:
   BSTNode();
   BSTNode(Key theKey, Value theValue) : 
   Key m_key;
   Value m_value;
   BSTNode* m_left;
   BSTNode* m_right;
};

 template <class Key, class Value>
    BSTNode<Key,Value>::BSTNode() : 
            m_key(Key()), m_value(Value()), m_left(0), m_right(0) {}

 template <class Key, class Value>
     BSTNode(Key theKey, Value theValue) : m_key(theKey), m_value(theValue), m_left(0), m_right(0) 
     {
        //...
     }

Note that the 0-argument version of the constructor initializes the key and value to whatever the default initial values are of the Key and Value types.

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
  • You were looking at a different version than I was talking about. Either way thanks for giving an actual answer. In the version I was talking about I had Key kk;\n k = kk\n Value vv; \n value = v;\n left = NULL; right = NULL; – Xerunix Apr 21 '15 at 15:36
  • Well, your latest version has the same issue as described by the answer. You're creating local variables and using them instead of the actual member variables. – PaulMcKenzie Apr 21 '15 at 15:38
  • k is the actual member variable though and I'm setting k to the local variable I created. I didnt know you could call the default constructor just with Key() though. That is definitely a better way to do it. The way they teach C++ at Miami is to just spend 3 weeks on it at the start of the data structures class and it is actually hard to know a lot of things about it in that time. We know a lot of java, but object creation is a bit different in java in my limited knowledge. – Xerunix Apr 21 '15 at 15:41
  • The `Key()` will create a temporary, default object of type `Key` and then initialize `m_key` to this value (or `k` using the one-letter variable). So if `Key` is an `int`, the default value for `int` is 0, if it's a `std::string`, then `Key()` will yield an empty string, etc. – PaulMcKenzie Apr 21 '15 at 15:44