0

How to deal with such task. I implemented BST template class which is capable to do operations on simple types like int, float etc. Now I want to implement another template class Vector and add different functionality for this type in BST template class because every comparison in case of Vector type compare Vector length not just value (in these case two values).

bst.h

#ifndef BSTTREE_BST_H
#define BSTTREE_BST_H

template <class T> class bst {
   struct node {
       T d;
       node *l, *r;
    };
    node* root;
public:
    bst();
    ~bst();
    void freeMemory(node* bstNode);
    void insert(T x); 
    void show(); 
    void show(node* bstNode);
    int count(node* bstNode);
    node* search(T x);
    node* search(node* bstNode, T x);
    bool rmv(T x); 
};

#include "bst.cpp"
#endif //BSTTREE_BST_H

bst.cpp

#ifndef BSTTREE_BST_CPP
#define BSTTREE_BST_CPP

#include "bst.h"
#include <iostream>

template <class T>
bst<T>::bst() {}

template <class T>
bst<T>::~bst() {}

template <class T>
void bst<T>::freeMemory(bst::node *node) {}

template <class T>
void bst<T>::insert(T x){}

template <class T>
void bst<T>::show() {}

template <class T>
void bst<T>::show(node* root) {}

template <class T>
int bst<T>::count(node* root) {}

template <class T>
typename bst<T>::node* bst<T>::search(T x) {}

template <class T>
typename bst<T>::node* bst<T>::search(node* root, T x) {}

template <class T>
node* bst<T>::rmv(int value, node *parent) {}

#endif //BSTTREE_BST_CPP

I didn't implement methods inner body, because it works just fine and it isn't the task. I know some things in this code are done wrong but I had to follow some rules which in my opinion aren't very clever, but it isn't the point.

I thought about making another template Class vector in .h/.cpp files and include header to Vector class in bst.cpp and write there additional functionality for only Vector class template. Is it a good idea? Or there is better way to do it (without changing my code very much if it is possible). How to do it?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
ssukienn
  • 558
  • 6
  • 22

2 Answers2

2

C++ supports operator overloading, this means that if you stick to using a specific comparison operator, eg *(root->l) < *(root->r) this will work correctly with primitive types as int, float, etc.

At the same time you can define your custom

bool Vector::operator<(const Vector& other) const { return this->length() < other.length(); }

So that the BST template class will correctly invoke your specified overloaded method when comparing Vector instances.

Jack
  • 131,802
  • 30
  • 241
  • 343
  • Ok, but what when I will want to insert a `Vector` object when every methods in bst template are designed to take signel value argument? I am bit confused... – ssukienn Dec 07 '15 at 19:05
  • @Saris Whatever operations on `T` your `bst` uses, need to be implemented in `Vector`. Most probably this is `<` (compare) and `=` (assign). Having ensured such operations exist, `bst` *just works* – Caleth Jun 15 '18 at 16:04
0

Instead of adding functionalities for a specific vector class, you can try creating an overloaded function where you compare the 2 values, and add a third argument to that function, which would be a function pointer, which you can define in your vector class.

Example ::

void func1(T v1, T v2, (bool) (*compare) (T, T));

(T is the template type)

And you can create this compare function, which might not only compare the lengths but also someother properties, depending upon the situation, and in func1 use the results returned by compare.

More about function pointers How do function pointers in C work?

Community
  • 1
  • 1
user007
  • 2,156
  • 2
  • 20
  • 35