For example,
I have an account generator class need a random method to decide username and password. Random has many ways to implement, so it is an interface with many subclass. Account generator class accept Random object in constructor.
In Java we don't care the deletion of object. But I'm now doing it in C++. My question is, if I did the following in main()
Random random=new StdRandom();
AccGen accGen=new AccGen(random);
should I delete "random" in main(), or I should put it in the destructor of AccGen?
Edit:
Even after the question is being answered very well, I think I may still state the reason that why do I use pointer, in order to help somebody facing the same problem.
The reason I use pointer, is to apply many patterns like Factory and Behavior. Non-pointer variable seems not able to be an Interface with pure virtual method, which cannot present overrides.
In a design view, I think the creator of an object is always responsible for its deletion. That is why it makes me confused. All the caller wants, it an AccGen object. I passed a random object is to decide the implementation of Random, inside AccGen. Then Random object is not a participant in caller's level, then why should the caller be responsible for its deletion. But the fact is, in some case, the random is useful in the following actions in caller's level. The given example is really just an example.
After a study of smart pointer, Unique pointer really is the solution of my problem. It makes pointer become more specific, in terms of to state the purpose of creating such pointer.
To summarize, if an object is useful after passed to constructor, then the type would be pure pointer and with no deletion in destructor. If the object is useless in caller's level, then the type should be unique pointer.