1
Object* a = new Object();

Object* b = a;

Is there a way in the Object class to know that 'b = a' happened? I tried overloading the operator= but it didn't seem to fire.

2
  • 1
    Why not just use std::shared_ptr? Commented Nov 15, 2013 at 18:37
  • Because I'm just playing around with this idea and was wondering if C++ informed me of this operation happening. Commented Nov 15, 2013 at 18:39

3 Answers 3

5

You overloaded the operator for Object not for Object*. You cannot overload operators for primitive pointers.

You will need to write a wrapper that simulates pointers but tracks operations. Perhaps you're looking for a smart pointer like shared_ptr? It implements reference counting like it seems you're trying to do.

Sign up to request clarification or add additional context in comments.

9 Comments

Bummer. I am looking for a smart pointer but I don't like wrapping pointers so was playing with possible alternatives. So no way to know when one pointer is set to another huh? That sucks. Thanks.
Oh is that actually true about not being able to overload operator = for Object*? I thought that was possible?
@user441521 Not realistically. A wrapper is your only reasonable option. Luckily the wrapper can be fairly transparent since * and -> can be overloaded to act on the wrapped pointer.
I don't see why you need to know when the assignment happens?
Another thing to keep in mind is that pointers are easy to overuse in C++ if you're not careful. You might want to step back at some point, decide why you're using a shared pointer and decide if it's correct. A shared pointer should only be used if multiple objects truly do own (as in are responsible for it's life cycle) one object. That tends to be a fairly rare situation in most programs. If you're just using smart pointers for easy life time management, than automatic lifetime (i.e. variables on the stack) and RAII are probably a better option.
|
0

Is this really what you want to do?

Seems to me that you want to copy the value of a to b?

I.e:

Object* b = *a;

Otherwise you are just setting b to point to a. You'd have to overload operator = for Object* rather than operator = as part of Object for a const Object&. Unless you really do want to copy the pointer in which case what you have is fine.

Note that overloading operator = for Object* is aparrently not possible! I doubt this is actually what you where really trying to do anyways.

Comments

0

I don't think this is going to invoke the copy constructor of Object class or use the '=' operator of Object class. It's just the assignment between two pointers of Object. So even you has overload the Object's '=' operator, which is not invoked in this case, you got nothing fired.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.