Option 1:
//A.h
class A
{
void foo()
{
}
};
Option 2:
//A.h
class A
{
void foo();
};
inline void A::foo()
{
}
Note that in option 2, the method is also implemented in the header (marked inline
to prevent multiple definition).
//A.h
class A
{
void foo()
{
}
};
//A.h
class A
{
void foo();
};
inline void A::foo()
{
}
Note that in option 2, the method is also implemented in the header (marked inline
to prevent multiple definition).
As you have presented it, no there is no difference. However, the 2nd form is sometimes used to take advantage of a declaration that might follow class A
's.
For example:
class A
{
void foo();
};
class B : A { };
inline void A::foo() {
B b;
}
In the first form, such a use of B
would be impossible.
A method defined inside the class definition is implicitly inline
, so, functionally, there is no difference.
Rob provides a good reason on one difference. Here's another, IMO.
Say you have:
class DBConnection
{
public:
DBConnection(const Config& config)
{
//some
//really
//complicated
//logic
//and
//checks
//and
//stuff
//I dunno
//not
//a
//db
//guy
//possibly
//exceptions
//thrown?
}
void reconnect()
{
//some
//really
//complicated
//logic
//and
//checks
//and
//stuff
//I dunno
//not
//a
//db
//guy
//possibly
//exceptions
//thrown?
}
void checkIntegrity()
{
//some
//really
//complicated
//logic
//and
//checks
//and
//stuff
//I dunno
//not
//a
//db
//guy
//possibly
//exceptions
//thrown?
}
void runQuery()
{
//some
//really
//complicated
//logic
//and
//checks
//and
//stuff
//I dunno
//not
//a
//db
//guy
//possibly
//exceptions
//thrown?
}
void dropTables()
{
//some
//really
//complicated
//logic
//and
//checks
//and
//stuff
//I dunno
//not
//a
//db
//guy
//possibly
//exceptions
//thrown?
}
void disconnected(Callback callback)
{
//some
//really
//complicated
//logic
//and
//checks
//and
//stuff
//I dunno
//not
//a
//db
//guy
//possibly
//exceptions
//thrown?
}
void selectTables()
{
//some
//really
//complicated
//logic
//and
//checks
//and
//stuff
//I dunno
//not
//a
//db
//guy
//possibly
//exceptions
//thrown?
}
Results getResults()
{
//some
//really
//complicated
//logic
//and
//checks
//and
//stuff
//I dunno
//not
//a
//db
//guy
//possibly
//exceptions
//thrown?
//YES, I copy-pasted these
}
//MANY MORE
}
Tell me, is there a way to get notified when the database is disconnected?
Now, try again with:
class DBConnection
{
public:
DBConnection(const Config& config);
void reconnect();
void checkIntegrity();
void runQuery();
void dropTables();
void disconnected(Callback callback);
void selectTables();
Results getResults();
//MANY MORE
};
inline DBConnection::DBConnection(const Config& config)
{
//some
//really
//complicated
//logic
//and
//checks
//and
//stuff
//I dunno
//not
//a
//db
//guy
//possibly
//exceptions
//thrown?
}
inline void DBConnection::reconnect()
{
//some
//really
//complicated
//logic
//and
//checks
//and
//stuff
//I dunno
//not
//a
//db
//guy
//possibly
//exceptions
//thrown?
}
inline void DBConnection::checkIntegrity()
{
//some
//really
//complicated
//logic
//and
//checks
//and
//stuff
//I dunno
//not
//a
//db
//guy
//possibly
//exceptions
//thrown?
}
inline void DBConnection::runQuery()
{
//some
//really
//complicated
//logic
//and
//checks
//and
//stuff
//I dunno
//not
//a
//db
//guy
//possibly
//exceptions
//thrown?
}
inline void DBConnection::dropTables()
{
//some
//really
//complicated
//logic
//and
//checks
//and
//stuff
//I dunno
//not
//a
//db
//guy
//possibly
//exceptions
//thrown?
}
inline void DBConnection::disconnected(Callback callback)
{
//some
//really
//complicated
//logic
//and
//checks
//and
//stuff
//I dunno
//not
//a
//db
//guy
//possibly
//exceptions
//thrown?
}
inline void DBConnection::selectTables()
{
//some
//really
//complicated
//logic
//and
//checks
//and
//stuff
//I dunno
//not
//a
//db
//guy
//possibly
//exceptions
//thrown?
}
inline Results DBConnection::getResults()
{
//some
//really
//complicated
//logic
//and
//checks
//and
//stuff
//I dunno
//not
//a
//db
//guy
//possibly
//exceptions
//thrown?
//YES, I copy-pasted these
}
Easier, right?
Not major differece between those two definitions.
in option1, that method is called as inline function, that is not allowing loops,and more lines of code. because of fast execution.
in option2, that is called as outside body definition, we can write any no.of lines.
i think inline means not prevent multiple definition. it means just do small amount of code without loops and etc.
No, there is no major difference. Both hint to the compiler that the method foo() may/should be inlined.
KerrekSB correctly points out that it takes more code, but there is of course an upside: It presents an option to separate "interface" from "implementation" even with inline methods.
E.g., You could present the class interface at the top of your header-file and clearly mark the remainder of the .h file as "inline implementations. Alternatively, you could include a second header file with the inline implementations.
//A.h
class A
{
void foo();
};
// "import" the inline implementations
#include "A.ipp"
And then have A.ipp as follows:
//A.ipp
inline void A::foo()
{
}
A small note of caution: It could cause some confusion with your co-workers because of a "third, non idiomatic C++ filetype": .h for classes, .cpp for "normal" source files and .ipp for inline implementations.