Posts

Showing posts from September, 2008

Optimizing tips for C++

Few tips in C++ to optimize the code: --> Data Types: 1. Using of 'int' instead of char or short. Its good to use the data type 'int' instead of 'char' or 'short' wherever possible. Explanation: Data type 'int' is always the native type for the machine. --> Operators 1. Arithmetic operations. Addition is always faster than multiplication and multiplication is faster than division. And bit wise operations are faster than any other Operation. Where ever possible see if you can write the optimized code. This optimized code, will save a lot of time and system resources.  2. Use 'op='. Wherever possible, use 'op=' in favour of 'op'. For example, use myShape += value; rather than myShape = myShape + value; Explanation:  The first version is better than the second because it avoids creating a temporary object. 3. if-else. Every 'if'  should have corresponding 'else' . Eg: if(a>b) {      someth

Aassigning to a reference?

What happens if you assign to a reference?   You change the referrent (the object to which the reference refers).  Remember: the reference IS the referrent, so changing the reference changes the referrent (a reference is an "Lvalue" [something that can appear on the "L"eft-hand-side of an assignment statement] for the referrent).  This insight can be pushed a bit farther by allowing references to be RETURNED. This allows function calls on the left hand side of an assignment statement, which is useful with operator overloading. 

Virtual Constructors.

What is a "Virtual Constructor"?   An idiom that allows you to do something that C++ doesn't directly support. You can get the effect of virtual constructor by a virtual  "createCopy()"  member fn (for copy constructing), or a virtual  "createSimilar()"  member fn (for the default constructor).   class Shape  {        public:  virtual ~Shape() { } //see on "virtual destructors" for more        virtual void draw() = 0;        virtual void move() = 0;  //...        virtual  Shape* createCopy() const = 0;        virtual Shape* createSimilar() const = 0;   };   class Circle : public Shape  {        public:  Circle* createCopy() const          {            return new Circle(*this);       }        Circle* createSimilar() const       {            return new Circle();       }             //...   };  The invocation of "Circle(*this)" is that of copy construction ("*this" has type "const Circle&" in these methods).  &

Difference between Pointer & Reference.

What are the differences between Pointers & References?  1. Both can be used to refer to any object of its type. But there is a difference in systax. Eg:   int *p=&a;   int &r=a; 2. A pointer can be NULL, whereas a reference has to be always pointed to some object. Eg:        For pointers:                 int *p; // This is valid.                 int *p=NULL; // This is valid.                 int *p=&a; // This is valid.       For references:            int &r; // This is In-valid.            int &r=NULL; // This is In-valid.            int &r=a; // This is valid.  3. A reference once it is binded to an object, it cannot be rebinded to a different object as, reference is inherently const.       Eg: int a, b;          int *p=&a;            p=&b; //  This is valid as it is a pointer.            int a,b;            int &r=a;            r=b; // This is invalid as a reference cannot be referenced to a different object.  Note: A reference can b