'inline' during declaration & definition

Q

qazmlp

// test.h
class test
{
public :
inline void check() ;

} ;

// test.C
inline void test::check() // Is 'inline' optional here?
{

}


Do I need to use 'inline' keyword again in the prototype in .C file
when it is already given as 'inline' in the .h file?.
 
E

ES Kim

qazmlp said:
// test.h
class test
{
public :
inline void check() ;

} ;

// test.C
inline void test::check() // Is 'inline' optional here?
{

}


Do I need to use 'inline' keyword again in the prototype in .C file
when it is already given as 'inline' in the .h file?.

No, you don't have to, but definition of the inline function should be
known in the source file where it is called so that the function body
can be inserted in hard-coded way. A common practice is to define
the function in class definition block of header file. That way, all the
sources including the header will know the definition of the function.
It's just "in a line" anyway.

class test
{
public:
void check() { /* do something */ } // inline keyword is not necessary
};
 
G

Greg Comeau

// test.h
class test
{
public :
inline void check() ;

} ;

// test.C
inline void test::check() // Is 'inline' optional here?
{

}


Do I need to use 'inline' keyword again in the prototype in .C file
when it is already given as 'inline' in the .h file?.

It's up to you.

But:
Normally you wouldn't use inline in the prototype in a .C file
because normally your inline would be in a .h file.

Also, adding inline to a definition to a function that is only
provided in a non-.h file and is expect to be used by other
non-.h source files usually will not obtain inline'ability,
since many compilers do not yet support cross translation
inlining. IOWs, this:

// file1.cpp

// ....
void inline foo()
{
...
}



//file2.cpp

//...
void bar()
{
foo();
}

will usually not consider foo as a candidate for inlineing.
 

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,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top