473,320 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Function declaration in class declaration

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
4 1906
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

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Roy Yao | last post by:
Why the following code let my compiler complain an overloaded function Init()? // code begin template<class T> class BicircularList { template<class T> class Iterator; template<class T> class...
6
by: Andrew Ward | last post by:
Hi All, Could someone please tell me why this code does not compile. struct A {}; struct B { B(A &) {} }; struct C {
2
by: Christophe Barbe | last post by:
I am not clear about friend functions of a template class. GCC (3.3.2) wants me to add <> after the friend function names in the class declaration and VisualC++ doesn't like that. template...
5
by: Trevor Lango | last post by:
What is the appropriate syntax for placing a friend function that includes as one of it's parameters a pointer to the class object itself within the template class? I have the following: ...
2
by: Ruben Campos | last post by:
I have a problem with a template function that is declared as a friend of a template class. I'll first show the exact problem with source code: // MyClass.hpp template <typename T> class...
3
by: gugdias | last post by:
I'm coding a simple matrix class, which is resulting in the following error when compiling with g++ 3.4.2 (mingw-special): * declaration of `operator/' as non-function * expected `;' before '<'...
1
by: yancheng.cheok | last post by:
currently, i have a private function in cat named privateFun. i would like to have this function "private" to all except dog's action member function. by using the following approach, all the...
4
by: Paulo Matos | last post by:
Hi all, I'm trying to work out a parser for function declarations but it turns out that it is harder than I initially thought. I'm looking at 3rd Ed of Stroustrup, page 808. I'm trying to parse...
4
by: nospam_timur | last post by:
Let's say I have two files, myfile.h and myfile.c: myfile.h: int myfunction(int x); myfile.c: #include "myfile.h"
4
by: fdmfdmfdm | last post by:
I have the following code: #include <iostream> #include <cstdlib> #include <cassert> using namespace std; template <class T> class Stack{ public: enum{DefaultStack = 10, EmptyStack = -1};
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.