Home arrow C++ arrow Page 5 - C++ Programming Tips

C++ Programming Tips

In this article, I give you programming tips that can improve your C++ code. I will cover use of parentheses, long expressions, modifying objects, and more. By the time I'm done, you will hopefully have learned a few ways to make your code faster as well as easier to understand and maintain.

Author Info:
By: Chrysanthus Forcha
Rating: 5 stars5 stars5 stars5 stars5 stars / 7
March 12, 2008
  1. · C++ Programming Tips
  2. · Modifying Objects
  3. · Advanced Parameter Passing
  4. · List
  5. · Pointers and Dynamic Memory

print this article

C++ Programming Tips - Pointers and Dynamic Memory
(Page 5 of 5 )

Pointer strength

The ability to access and change the value of an object whose name is not part of the assignment statement is the feature that makes pointer objects so powerful. This capability is most often used for dynamic objects.

Inheritance: Using data member initialization list

There are two ways to initialize data members when implementing the constructor of a class. One way is to invoke the constructor for the data member via the data member initialization list. The other method is to invoke the data memberís mutator from within the body of the constructor. With the second method, the data memberís default constructor is invoked to construct an object with default values, then the mutator is called to overwrite the default values with initial values. Using the first method, the data member is constructed with the appropriate initial values. As you can see the second method is more efficient, because it is done during construction.

Templates and Polymorphism

Automatic subscript checking for vector elements and other list elements

The vector class template provides a member function, which is:


The function expects an index value as its parameter, say i. If the index is valid (exists), a reference to the ith element of the vector is returned. Otherwise, an exception is generated; the program stops, giving an error message that the user does not understand. You can use inheritance to derive a class that overloads the subscript operators so that index checking is automatically performed in conjunction with the subscript operators. To solve this problem, a template class named protectVector is given below.

Template <class T>

class protectVector : public vector<T>



protectVector() : vector<T>() {}

protectVector(int n) : vector<T>(n) {}

protectVector(int n, T v) : vector<T>(n,v) {}

T& operator[](int i)


return at(i);


const T& operator[](int i) const


return at(i);



Assume reuse

If you are defining a class, unless you know it can never serve as a base class for a derive class, give the class a virtual destructor. In this way, all derived destructors will also be virtual and then, whenever an object is deleted, the correct destructor will be invoked, regardless of the context.

All these tips are what I have gathered, based on my experience and the experience of expert programmers. Here's hoping you put them to good use.

DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials