0

Is this good design or not ? Please suggest

Hi I have a class which is abstract and has three child class. There is a static member taking int as input and returns a base class pointer ehich is abstract class declared above (Basically a factory patrern). Now factory pattern taking int decides which class pointer to return. Based on int, I am querying another class data and decides whether object of that class is possible or not. Once object is obtained or not. If object is valid, it helps to identify which derived class pointer is to be returned. If object is not valid, is it good practice to return nullptr or not ?

19th May 2023, 6:00 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
3 ответов
+ 3
Returning nullptr should be fine as long as it's handled correctly on the receiving end, afaik. Return a bare NULL was/is a headache sometimes, but I don't think the pointer version is as problematic.
19th May 2023, 11:59 PM
Orin Cook
Orin Cook - avatar
+ 1
If you delete ptr, you can make it as zero int *ptr; ptr = new int(); *ptr = 25; cout<<ptr; delete ptr; ptr = 0; something like that, but it's depends what you exactly has to do
19th May 2023, 8:47 PM
Smith Welder
Smith Welder - avatar
+ 1
Thanks for your views Orin Cook and Smith Welder However, could you plz explain more smith ? I could not get your point specially where delete came into picture for int*?
20th May 2023, 4:21 AM
Ketan Lalcheta
Ketan Lalcheta - avatar