473,507 Members | 13,597 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Member functions inside structure blocks?

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++?
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;

....

A::A(int q1, int q2)
{...}

A::~A()
{...}

Jul 23 '05 #1
3 1589
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
Jul 23 '05 #2
On 16 May 2005 06:01:01 -0700, ja******@my-deja.com wrote in
comp.lang.c++:
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++?
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;

...

A::A(int q1, int q2)
{...}

A::~A()
{...}


There is no such language as "C/C++", and your post is proof of it.
This code is legal, but of doubtful use in C++. It is totally illegal
in C.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 23 '05 #3
Hi

Is there really any use of the typedef ? Not sure.

This practice of putting functions inside a struct definition is valid
in C++ but illegal in C.

Just remember though in a class the default access specifier is private
while in a structure it is public.

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2828
by: Wenjie | last post by:
Hello, I read someone posted assertions that even the (public) member function is not static, there are probably only one copy of the code in the executable. Then except the...
2
2815
by: tekenenSPAM | last post by:
I know that one cannot cast a non-static member function to a void *. (http://users.utu.fi/sisasa/oasis/cppfaq/pointers-to-members.html) However, I have a case where I need to call a certain...
12
2062
by: Anthony Jones | last post by:
Just a bit of background: I'm one of a group of FORTRAN programmers, looking to switch to C++. We are trying to write a few simple examples to demonstrate the power of the language to our manager,...
2
2239
by: cppaddict | last post by:
Hi, I have the following line inside one of my class member functions: static bool tested = false; if (!tested) { MessageBox(NULL, "Hi", "Testing", MB_OK | MB_ICONEXCLAMATION); tested =...
7
2163
by: jolie.demiranda | last post by:
Dear all, I'm fairly new to C++ so forgive me is this topic may have been convered before... I'm having a problem with pointers to member functions. Essentially what I want to achieve is the...
15
2049
by: roberts.noah | last post by:
I ran across some code that called memset(this, 0, sizeof(*this)) in the member function of a structure in C++. This just looked wrong to me so I did some web searching and it does indeed seem...
1
4360
by: ypjofficial | last post by:
Hi all, what's difference does it make when we define the member function inside and outside a class? I am using vc7. when i define the member functions outside a class with the scope...
27
5472
by: arkmancn | last post by:
Any comments? thanks. Jim
7
3946
by: Immortal Nephi | last post by:
My project grows large when I put too many member functions into one class. The header file and source code file will have approximately 50,000 lines when one class contains thousand member...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7110
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5041
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1540
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.