answersLogoWhite

0

Member functions must always be declared inside a class declaration, however they may be defined either inside or outside of the class. A definition is simply the implementation of a function, the code that is executed when the function is called.

When a function is defined inside a class declaration then it is implicitly inline expanded. When it is defined outside of a class declaration, it is not inline expanded but you may explicitly declare it to be inline expanded, if desired. Note that inline expansion should only be utilised when the function has but a few simple statements, preferably just one or two statements at most.

The following example demonstrates the definition of a typical class accessor (a getter) defined within a class declaration (where inline expansion is implied and desired):

class A {

public: int get_data()const{return(m_data);}

private: int m_data;

};

The following example shows the same function defined outside of the class. This time the function will not be inline expanded.

class A {

public: int get_data()const;

private: int m_data;

};

int A::get_data()const{return(m_data);}

Note that the definition may appear in a different file. Generally, classes are designed with a header file and a source file, where the header contains the declarations and the source contains the definition. The source file must include the header file.

Since it is often desirable to inline expand simple class accessors that merely return values, the inline keyword can be used when the definition is external to the class declaration, like so:

class A {

public: inline int get_data()const;

private: int m_data;

};

int A::get_data()const{return(m_data);}

Note that declaring a function inline (implicitly or explicitly) is no guarantee that it will actually be inline expanded, you are merely signalling to the compiler that the function is a candidate for expansion. The compiler is still free to veto the promotion if its inline optimisers deem that such an expansion would compromise performance due to the increased code size that inline expansion incurs. Functions that are called in only one place in your code, regardless of how complex they are, are generally good candidates for expansion. Although you could manually inline expand such functions, if the function call makes your calling code easier to read and maintain, then it's better to retain the function in your code.

Note also that while some compilers allow you to force an inline expansion (such as Microsoft's __forceinline keyword), effectively bypassing the compiler's optimisers, this should be done sparingly as the increased code size can easily outweigh any performance gained by removing the function call. Also note that some functions cannot be inline expanded, even by force. In particular, the compiler cannot inline expand any of the following:

  • a function with a variable argument list.
  • a recursive function (see * note below).
  • a function compiled for debug builds.
  • where the function and the caller use different types of exception handling.
  • a function that uses inline assembly (often dependant upon compiler flags).
  • a virtual function that is called virtually rather than directly.
  • indirect calls via function pointers.

* Some recursive functions can be inline expanded up to a predetermined depth, usually 16 calls at most (thereafter, the calls are treated as calls to new instances of the function). The predetermined depth generally cannot be increased, but it can typically be reduced with a pragma.

For more specific information on inline expansion within your compiler or IDE, consult the compiler's documentation regarding the inline keyword.

User Avatar

Wiki User

12y ago

What else can I help you with?

Continue Learning about Engineering

Why all variables are declared with in the function?

It is not necessary to to declare variables inside the function in C. If you declare a variable inside a function, the variable becomes local for the function and another variable of same name can be declared in any other function, but you can not use the variable declared in other function. When you declare any variable outside the function body then the variable becomes global and can be used in any function of the program. Note: errno is an example for a variable declared outside any function.


A variable declared inside the for loop control cannot be referenced outside the loop?

Yes. A variable declared inside the loop is a local variable for the code block enclosed by the {} statements of the for loop. The variable will not be available to be used by any code outside the code block.


When variables are declared in the body of a method they are know as what?

Local Variables There are two types of variables based on the location of declaration 1. Instance Variables- Declared inside a class, but outside of any method's body. 2. Local Variables- Declared inside a method's body inside a class.


How can you declare global and local variable in pseudocode?

A local variable is a variable declared inside a construct, such as a class or function, while a global variable is a variable declared outside of any construct.


Are arrays in C created on the stack or the heap?

That depends on where you define them. Arrays defined inside functions are declared on the stack (like other variables defined in functions). Arrays defined outside of any function, or using the static keyword inside a function are allocated in the static data area of the program. Other arrays may be allocated using malloc() (or "new" in C++); these are allocated on the heap.

Related Questions

Why all variables are declared with in the function?

It is not necessary to to declare variables inside the function in C. If you declare a variable inside a function, the variable becomes local for the function and another variable of same name can be declared in any other function, but you can not use the variable declared in other function. When you declare any variable outside the function body then the variable becomes global and can be used in any function of the program. Note: errno is an example for a variable declared outside any function.


How do you declare a constant using PHP?

Outside of a class definition, constants can be declared with the define function. The function has two arguments; the name of the constant (a string), and the value of the constant (any variable type).Inside a class definition, constants can be declared with the const keyword. Type the word const, the name of the constant in the form of how it will be called later, an "equals sign," then the desired value of the constant.In both cases, here is an example.


Where do exoenzyme function inregard to the bacteria inside or outside the cell?

inside


A variable declared inside the for loop control cannot be referenced outside the loop?

Yes. A variable declared inside the loop is a local variable for the code block enclosed by the {} statements of the for loop. The variable will not be available to be used by any code outside the code block.


What is the function of the fuselage?

The fuselage keeps the inside in and the outside out.


What is the definition of parent function?

When a function is nested inside another function, the outer one is the parent, the inner is the child.


What is the difference between variable declared inside the declaration part and variable declared in scriptlet part?

Variable declared inside declaration part is treated as a global variable, which means after translation of jsp file into servletthat variable will be declared outside the service method as an instance variablethe scope is available to the complete jspVariable declared inside a scriplet will be declared inside a service method as a local variable and the scope is with in the service method.


What is the difference between a static variable a global variable and a local variable?

A static variable is a variable allocated in static storage. A local variable is a variable declared inside a function. A global variable is a variable declared outside of any class or function. Note that local variables and global variables can both be allocated in static storage.


When variables are declared in the body of a method they are know as what?

Local Variables There are two types of variables based on the location of declaration 1. Instance Variables- Declared inside a class, but outside of any method's body. 2. Local Variables- Declared inside a method's body inside a class.


Difference between member function and friend function?

1. Function - is normally refered to a C-style function which has a global level scope. As long as its declaration is visible in a file where it is being used, and the definition is available somewhere in the application, the linker will find the definition and link to it. It can be used anywhere in the application. 2. Member function - is normally referred to a C++Style method declared/defined inside of a C++ class. The scope for such member functions is the class. They are not accessible outside the class and are only accessible thru an object/instance of such a class. There are, of course, exceptions to this, such as static and friends.


Can variable declared in one scriplet access in another scriplet?

No. Variables declared inside a scriptlet are like method local variables which are not accessible outside the scriptlet/method.


How can you declare global and local variable in pseudocode?

A local variable is a variable declared inside a construct, such as a class or function, while a global variable is a variable declared outside of any construct.