Hi I've trying to improve the maintainability by replacing a piece of C++ code from procedure to using classes with chain of responsibility design pattern (procedure codes are move to the classes).
Since I'm replacing it with a class that is instantiate with the new keyword and the pointer is deleted after using and this repeat every time, My concern is there a performance degrade compare to the current procedure code here?
It's hard to say without seeing both versions. And even then the only way to know for sure would be to try it and see.
One thing I will say, though, is only allocate with new if there's reason to. If you weren't dynamically allocating memory before, there probably isn't any reason to do it now.
C++ was designed to be as fast as C, so it's possible to do encapsulation without incurring a runtime cost. However, if you're newing and deleting your wrapper where the old C stuff didn't, then you've likely done something different and will have that extra runtime cost.
There are so many things that can decrease the performance of a program. I suggest implementing both versions and using the profiler to check.
Using "new" and "delete" in loop are in most chases a bad idea. Try design your program to only use "new" and "delete" and the start and end of your program.
Another problem. How will my newly created class (in a header file) reference the current class (in a c file) protected const class variable which one of it's field is initialize at constructor?
I tried changing from protected to public and making the variable static but it give me a compilation error.
1 2 3 4 5 6 7 8 9 10 11 12
class MyCurrentClass {
protected:
const MyVariable v; //changed to static const MyVariable
public:
MyCurrentClass() : v("") {} //v initialize at constructor
void currentProcedureCode() { //code here moved to another class that uses chain of responsibility design pattern
//reference v here
}
}