ja******@my-deja.com wrote:
I saw some code written where member functions were written inside
struct definition blocks and was wondering if this is an advisable or
common style of programming in C/C++?
It depends.
The type of code I saw compiled under the g++ compiler and looked like
this:
typedef struct A
{
int x;
int y;
A(int, int);
~A();
} a;
This is not A GOOD IDEA(tm). What's the typedef for? Do you really need
the 'a' alias for the 'A' struct?
...
I suppose here in the '...' lies the boundary between the header file and
the compilation unit.
A::A(int q1, int q2)
{...}
A::~A()
{...}
Both methods (defining member functions inside the class definition or in
a separate translation unit) are acceptable and get used intermittently by
all C++ programmers, AFAICT.
If you like them separate (which improves the readability of the class
definition, IMO), but still want the member definitions to be in the same
header file as the class definition, the functions should [usually] be
declared 'inline' when defining them.
V