0

I have a Binary Search Tree, I use template to add any class object to it.

I have a search function that returns said class object or NULL

Do I need to overload "=" to return this object ? I was hoping if I return object it would know to check: If same type replace values stored in LHS with values stored in RHS and if NULL mark first object as NULL.

Is my mistake maybe somewhere else (all I return is either full object or NULL, nothing else) or do I actually have to overload it ?

I have limited time (very) so how can I do this if needed ? and is it a fast process or will it involve a lot of modifications.

Sorry for lack of code but I can't think of any that would be relevant.

EDIT I also use a lot of NULL so can I return NULL into an object ? Example:

class Matrix {
   private:
     int col;
     int line;
     int value;
}

Matrix mat; mat = NULL;

Some code:

template <typename Type>
Type BST<Type>::search(int key) {
    if (this->root == NULL)
        return NULL;
    else
        return root->search(key);

Here Type is Matrix. Can I return NULL or go further with search and return Type, which again is Matrix?

Note: This is for homework purposes, leaking memory is my last concern. Simplicity and speed is by far my first problem

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Kalec
  • 2,681
  • 9
  • 30
  • 49
  • 3
    I think the code would be relevant, it's not really clear what you want. Why would you need to overload `=` to **return** an object? You use `=` to assign to an object (albeit it does have a return). – Luchian Grigore Jun 14 '12 at 10:05
  • 1
    with the amount of information you provide about the subject it is hard to say what you need. in general, C++ compiler generates a deep (calling assignment operators on members) assignment operator code automatically. – bobah Jun 14 '12 at 10:07
  • 1
    STL container search functions return an iterator; if the iterator has a certain magic value (typically equal to `container.end()`) the search is considered to have failed. Another possibility is to override the cast to `bool` on your object so a special class instance representing an empty or nonexistent node will evaluate as `false`. – Rook Jun 14 '12 at 10:08

2 Answers2

2

No, you cannot assign NULL to an object.

Matrix mat; mat = NULL;

Is illegal in your case. If you want to be able to have NULL, you can use pointers instead (raw or smart).

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • so can I make `Matrix *mat = new Matrix` then `mat = NULL` ? – Kalec Jun 14 '12 at 10:10
  • 2
    @Kalec yes, but then you leak memory. You have to manually `delete mat;` when you no longer need it. – Luchian Grigore Jun 14 '12 at 10:12
  • 3
    @Kalec: You should care if your tutor does. If he doesn't, then you should drop the course - it won't teach you how to write C++. – Mike Seymour Jun 14 '12 at 10:15
  • @Kalec but, as stated, no, you can't return NULL for an object. You probably want to return a pointer. – Luchian Grigore Jun 14 '12 at 12:34
  • @LuchianGrigore ohhh, I'm just sooo clumsy, sorry :) – Kalec Jun 14 '12 at 12:35
  • @MikeSeymour I'm not asking you to. I do realize that memory handling is important, I know c++ is a very good programming language, I also want to learn it. Problem is we have a lot of homework, as in an absurd quantity. Last semester we had just as much, but we did it in python and it was manageable. However c++ is a big and dense programming language that demands a lot of knowledge, more than we can possibly learn in a semester, and since we are graded on quantity over quality, I have to submit and do as much as possible in my limited time. I am sure you can understand that – Kalec Jun 14 '12 at 12:37
  • @MikeSeymour I also use the homework tag to suggest I have a non important program on a very small deadline, I used to mention that in the question but it kept get edited out, so I reverted to the tag. I hope you didn't take any offence since none was intended. – Kalec Jun 14 '12 at 12:37
  • @LuchianGrigore I have fixed to problem by returning a pointer as mentioned – Kalec Jun 14 '12 at 12:38
1

To return an object from a function, it must have an accessible copy or move constructor. It does not need an assignment operator; that's only needed for assignment. Of course, you will need that if you're assigning the function result to a previously declared variable.

Note that a public copy constructor and copy-assignment operator will be generated automatically, if you don't delcare them yourself.

However, if your object is managing resources that are freed in its destructor, then you'll need to consider the Rule of Three. You should either implement or delete the copy constructor and assignment operator; otherwise, it's very easy to accidentally introduce memory leaks, and worse, to have two objects both trying to free the same resources.

To answer the new question, you can't return a null pointer in place of an object. You could either return a pointer (either a raw pointer to something contained in the tree, or a smart pointer like std::unique_ptr<Type> to a newly allocated copy, to make memory leaks less likely), or a nullable object type such as boost::optional<Type>.

Community
  • 1
  • 1
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644