Click here to Skip to main content
Click here to Skip to main content
Go to top

WWWH (What When Why & How) on C++

, 26 Feb 2004
Rate this:
Please Sign up or sign in to vote.
This article contains most of the questions/answers for various concepts of C++ such as Constructors, Destructors, Virtual Functions & Destructors, Operator overloading and many more

Introduction

This article contains question on C++ concepts based on WWWH format. When there are more lots things (say in life) or in Studies then we have to break it into small things, and here I had applied the technique called as basically WWWH Funda which is nothing but divide and rule.

Background

Readers should be familiar with basic concepts of object oriented programming.

Using the code

For simplicity we consider one example for generating the questions on inline function & macro using WWWH technique. Using this technique we may generate more questions to learn & understand the concept of inline function & macro.

Following are the questions

1. What is an inline function?

An inline function is a function whose code gets substituted in lieu of the actual call to the function.

2. What is the purpose of inline functions?

In some cases, inline functions make a compute-bound application run faster.

In a broad sense, the idea behind inline functions is to insert the code of a called function at the point where the function is called.

3. Are there any special rules about inlining?

Yes, here are a few rules about inlining.

  • Any source file that contains usage of an inline function must contain the function's definition.
  • An inline function must be identically defined everywhere. The easy way to do this is to define it once, preferably in the class header file, and include the definition as needed.
  • main() cannot be inline.

4. How to define a function as "inline"?

Following is syntax to define a function as inline -

inline return-type function-name(arguments);

//
// Example to define a function as inline        
       inline int MyFunction();

5. How to make a function as a default inline function without specifying inline keyword?

Simple, just define that function inside the class definition.

//  For example
    class Demo {
                      public: 
             int num;    
            intMyNumber() {
                return num; }

6. Does inline functions are virtual functions?

No, you cannot define the inline functions as virtual functions.

7. Why inline functions are not virtual functions?

Inline functions are not virtual functions because they are bound at the compile time, where as virtual functions are bound at run-time.

8. Which functions are treated as inline function?

Compiler-generated default constructor, copy constructors, destructors, and assignment operators are treated as inline.

9. How the inline functions can speed up the processing

Inline functions can speed up processing in 3 ways -

  • Eliminating the overhead associated with the function call instruction.
  • Eliminating the overhead associated with the pushing and popping parameters.
  • Allowing the compiler to optimize the code outside the function call more efficiently.

10. Which are the situations where inline function is not working?

Some situations where inline function may not work -

  • For function returning values, if a loop, a switch or goto exists.
  • for function not returning values, if a return statement exists.
  • if functions contains static variables.
  • if inline functions are recursive.
  • if function contains large code.
  • if function is virtual as binding takes place at compile time.

11. What is a macro?

A macro is a name that will be substituted by a specified text wherever the name appears in the source file. this replacement is done by the pre-processor.

12. What is the disadvantages of macro over inline function?

  • Macro is not really a function and so usual error checking does not occur during compilation.
  • Macro is expanded by preprocessor while inline function is parsed by compiler.
  • Expressions passed as argument to inline functions are evaluated once. In some cases, expressions passed as arguments to macros can be evaluated more than once.

13. What are the side effects of using macro instead of inline function?

The use of macro involves literal substitution of the macro-name and its parameters by the macro-body and arguments. when a macro is used in an arithmetic expression, unexpected results may be produced due to operator precedence.

Following code show the side effects of macros

#include <stdio.h>
#define SQUARE(x)    x * x
#define SQR(x)     ((x) * (x))
void main(void) {
        int num1 = 10, num2 = 2;
        int wrongsqr, rightsqr;         
        wrongsqr = SQUARE(num1 + num2);
        rightsqr = SQR(num1 + num2);
        printf(" Wrong SQR = %d \n", wrongsqr);
        printf(" Right SQR = %d \n", rightsqr);
           }             
        

The output is as follows -

Wrong SQR = 32
Right SQR = 144

This is what I developed such questions on inline function & macro, you can generate more questions based on this question set.

References used for this articles

  • Object Oriented Programming with C++ - By E. Balagurusamy
  • C++ FAQ (2nd Edition) - Marshall Cline

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Dinanath
Technical Lead
India India
Dinanath has total IT experience 9+ years

Comments and Discussions

 
Generala fine article Pinmemberavenger_sb2527-May-04 22:54 
GeneralCopy Constructors PinmemberS. Senthil Kumar5-Mar-04 22:36 
GeneralRe: Copy Constructors Pinmemberbhalchan26-Jul-05 7:45 
GeneralRe: Copy Constructors PinmemberS. Senthil Kumar26-Jul-05 9:00 
QuestionWhy should you prefer ++i to i++? PinmemberJohn R. Shaw3-Mar-04 9:34 
Generalvery concise and useful article Pinmemberedger1-Mar-04 19:32 
GeneralYou can have inline virtuals PinmemberJason King27-Feb-04 7:35 
GeneralRe: You can have inline virtuals PinmemberPrabhaGovind5-Jul-07 20:19 
GeneralMisleading title PinmemberKevinHall27-Feb-04 5:27 
GeneralRe: Misleading title PinmemberKevinHall27-Feb-04 5:35 
GeneralRe: Misleading title Pinmemberdinanath28-Feb-04 19:08 
Generalinline is not necessarily inline PinmemberMartyn Pearson26-Feb-04 23:00 
GeneralRe: inline is not necessarily inline Pinmemberdinanath28-Feb-04 19:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140922.1 | Last Updated 27 Feb 2004
Article Copyright 2004 by Dinanath
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid