What are some basic c++ programming techniques?

What are some C++ programming techniques and examples that should be discouraged or avoided?

  • What are the natural bad habits that a cpp programmer can run into?

  • Answer:

    Thou Shall Not Program like, IBMWFactory = IGermanyBMWCarFactoryFactory->CreateFactory(); IM3Factory = IBMWFactory->CreateM3Factory(); Class M3Car; M3Car = IM3FactoryVistor->VisitFactory(IM3Factory)->GetSalesDept()->GetSalesGuy()->PlaceM3Order().UrgentOrder();

Tamil Selvan at Quora Visit the source

Was this solution helpful to you?

Other answers

Scott Meyers mentions a lot of pitfalls and how to avoid them in his series of books (http://www.aristeia.com/books.html) This is, hands down, the best resource for learning best practices in C++

Chander Shivdasani

1. Using void main() when the standard clearly says int main()     http://users.aber.ac.uk/auj/voidmain.cgi 2. http://en.wikipedia.org/wiki/Dangling_pointer 3. Using macros when you could use const.     http://stackoverflow.com/questions/319452/are-all-macros-evil 4. Writing compiler dependent code     like x= ++a + a++ + a++ 5. Using age old Turbo C++ in place of standard http://gcc.gnu.org/! 6. (GCC specific) not using -Wall and other important options     which can detect common mistakes. 7. Forgetting the 'delete' when using 'new' in dynamic memory allocation. 8. Unwarranted use of global variables. 9. Not using standard library.

Dhiraj Thakur

Using unspecified numbers of arguments in functions.

Hayk Baluyan

Using pointers correctly and carefully remembering to deallocate the resources to which they point at every possible place they go out of scope, including when catching exceptions. This is bad because you should just use a boost:shared_ptr<T> and save yourself all the hassle.

Tom Allen

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.