84.0k views
3 votes
How would we declare a method defined outside the class declaration to be inline

A) inline void methodName();
B) void inline methodName();
C) void methodName() inline;
D) void methodName();

User Odan
by
7.5k points

1 Answer

3 votes

Final answer:

To declare a method as inline when defined outside the class declaration, option A) inline void methodName(); is correct. The inline keyword should be placed before the function's return type in both the declaration and the definition.

Step-by-step explanation:

To declare a method as inline when it is defined outside the class declaration in C++, you should include the inline keyword before the function return type when you provide the definition of the method. Among the options provided, the correct syntax is A) inline void methodName();. When you define the function, you should also repeat the inline keyword before the function return type in the definition, outside the class. For example:

class MyClass {
void methodName(); // Declaration inside the class
};

inline void MyClass::methodName() { // Definition outside the class
// ...method implementation...
}

Using the inline keyword suggests to the compiler that the function should be inlined, which means replacing the function call with the actual code of the function, possibly optimizing the program for speed.

User Diandra
by
7.6k points