Function declaration in class declaration

O

Ovidesvideo

Just a quick question. If I declare functions in the class declaration as such:

class myclass
{
public:
void function(int &x)
{
x++;
}
};

Will the function be called normally, or simply inlined in the code?
 
R

Rob Williscroft

Ovidesvideo wrote in in comp.lang.c++:
Just a quick question. If I declare functions in the class declaration
as such:

class myclass
{
public:
void function(int &x)
{
x++;
}
};

Will the function be called normally, or simply inlined in the code?

That is implementation defined (i.e. its upto your compiler), but this
rule applies to *all* inline functions. Many compilers have options that
let you control how they handle this situation so read the manual.

When compiling debuggable code some compilers won't inline anything.

Some compilers (when optimizing) will inline functions that wern't
defined as inline.

But for normal (non-debug) code you can reasonable expect that small
functions like your example will be inlined, you just can't rely on it.

Rob.
 
G

gianguz

The function will be inline expanded.
This is not an implementation defined behaviour but it is part
of the semantic of inline declarations.

Look at the following:

class A {
public
A() {}
inline void a();
void b() {}
};

A::a() {}

a() and b() are semantically equivalent.

Gianguglielmo
 
A

Andrey Tarasevich

Ovidesvideo said:
Just a quick question. If I declare functions in the class declaration as such:

class myclass
{
public:
void function(int &x)
{
x++;
}
};

Will the function be called normally, or simply inlined in the code?

A function declared in this fashion is an 'inline' function. Whether the
calls to this function will actually be inlined depends on the
compiler's decision. Moreover, this decision can be made independently
for each call to this function - some calls might get inlined and some not.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top