By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,473 Members | 3,229 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,473 IT Pros & Developers. It's quick & easy.

Function declaration in class declaration

P: n/a
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?
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Ovidesvideo wrote in news:20***************************@mb-m17.aol.com
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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2

P: n/a

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

Jul 22 '05 #3

P: n/a
gianguz wrote in news:11*********************@f14g2000cwb.googlegro ups.com
in comp.lang.c++:
The function will be inline expanded.


The Standard says this is *prefered* not *required*.

See 7.1.2/2

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #4

P: n/a
Ovidesvideo wrote:
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.

--
Best regards,
Andrey Tarasevich
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.