Changes between Version 11 and Version 12 of code/PerformanceTips
- Timestamp:
- Apr 12, 2017, 10:18:11 PM (8 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
code/PerformanceTips
v11 v12 1 1 = C++ Performance Tweaking Tips = 2 [[TracNav(TracNav/TOC_Development)]]3 2 4 3 == General Idea == … … 68 67 69 68 === const functions === 70 Now you may think of a situation, where OtherClass has aMyClass as a membervalue:69 Now you may think of a situation, where !OtherClass has a !MyClass as a membervalue: 71 70 {{{ 72 71 class OtherClass … … 85 84 Vector3 position = instance.getObject().getPosition(); 86 85 }}} 87 But this doesn't work. Why? Because OtherClass returns a '''const''' reference to MyClass. This means, you can't change anything inMyClass. But getPosition() doesn't change anything? You're absolutely right, but the compiler doesn't know about that. You have to tell him by adding the const keyword to the function head as well:86 But this doesn't work. Why? Because !OtherClass returns a '''const''' reference to !MyClass. This means, you can't change anything in !MyClass. But getPosition() doesn't change anything? You're absolutely right, but the compiler doesn't know about that. You have to tell him by adding the const keyword to the function head as well: 88 87 {{{ 89 88 #!html … … 106 105 * Add '''const''' to all functions that don't change the class. 107 106 108 And remember: '''const ObjectName&''' might look scary, but it's your friend. ;)107 And remember: '''const !ObjectName&''' might look scary, but it's your friend. ;) 109 108 110 109 == Memory Allocation and Deletion: new, delete == … … 156 155 } 157 156 }}} 158 Of course SomeClass must now delete the object reference if it isn't needed anymore.157 Of course !SomeClass must now delete the object reference if it isn't needed anymore. 159 158 160 159 == Redundant code ==