0
Remove raw pointer from observer design pattern
Hi I have implemented an observer design pattern which has You tube channel as subject and three humans as observers. This works good as per below code: I was asked to remove the raw pointer from implementation and go with smart pointer. How to do so specially when I want to have object (not pointers ) in my client code (main function in this case)? Also, Person(const string& name,YoutubeChannel* pChannel) class constructor has dependency on concrete class rather than iSubject. Is this okay? If not, what is best way to design this? https://sololearn.com/compiler-playground/c5ANFvk4q6Qz/?ref=app
4 Antworten
0
maybe use shared_ptr ?
https://sololearn.com/compiler-playground/c2rwVcQOlyf8/?ref=app
0
Nope Bob_Li
Making class having smart pointer forces a client to create the pointer only.
It does not work with simple class object as I have in my original code's main function.
I just want to allow client to either use pattern with class object or smart pointer. Not only with smart pointer alone.
0
oh, that's different from what i thought i was reading... now I'm confused...
you want to modify the function so that it can accept a pointer or an object as argument? Maybe it's better to define two overloaded functions instead?
0
Seems you are too much confuse Bob_Li
I don't have any issue in main function. Let it have objects of youtubechannel and person class. DONT CHANGE THIS I.E. KEEP MAIN FUNCTION AS IT IS.
Now, if you observe the class related to isubject and person class , it has raw pointers. I don't want to have raw pointers here . Is there a way to get rid off the raw pointers ? I know we can have smart pointers here , but make sure this change in isubject and person class make you force to change the main function