Tutorial :Better alternative for c++ old feature?



Question:

c++ has come a long way, it has lot of feature that lead to do same task in n number of ways. What feature do you think should be avoided and list better alternative for the same.

Like use SmartPointers in places of pointers


Solution:1

Avoid malloc, use new.

Avoid memcpy, use a copy constructor.

Avoid #defines, use consts.

Avoid (C-style) casts, use static_cast<C++>( style casts ).

Avoid macros, use templates.

Avoid post-increment if you can use pre-increment.

Avoid new, use collections of value types, let the collection deal with memory management.

Avoid pointers to new'd objects, use boost::scoped_ptr or boost::shared_ptr.

Avoid writing your own code to traverse collections, use <algorithm>s

Avoid reinventing the wheel, see if boost has what you need.

Avoid "char * name" for storing strings, use std::string.

Avoid using namespace foo, use using foo::bar.

Avoid "static (internal linkage) declarations" use unnamed namespaces.


Solution:2

use new style casts

static_cast, const_cast, reinterpret_cast, dynamic_cast,

instead of C style casts which are no longer considered fully safe

also prefer using STL containers over dynamically resizing arrays at runtime


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »