c++ struct allowed to have method?

C

Carfield Yim

I've tried in gcc, it is allowed to have methods in a struct, so this
is same as a class, is it?
 
B

Balog Pal

Carfield Yim said:
I've tried in gcc, it is allowed to have methods in a struct, so this
is same as a class, is it?

Yes, except for default accessability is public in struct, private in class.
 
S

Suraj

I've tried in gcc, it is allowed to have methods in a struct, so this
is same as a class, is it?

C++ is backwards compatible with C struct declarations. I think
backward compatibility is the biggest reason for retaining structs.

The only difference i am aware of is that structure members have
public access by default and class members have private access by
default
 
P

pauldepstein

C++ is backwards compatible with C struct declarations. I think
backward compatibility is the biggest reason for retaining structs.

The only difference i am aware of is that structure members have
public access by default and class members have private access by
default

The FAQ alludes to another difference but it's more a cultural
difference than a legal one. According to the FAQ, most programmers
use the word struct for a particularly simple class -- for example one
which doesn't inherit and which won't form a base of any inheritance
hierarchy.
That was the convention of my previous company -- the keyword struct
was used for a simple class.

Paul Epstein
 
A

Andrey

The FAQ alludes to another difference but it's more a cultural
difference than a legal one.  According to the FAQ, most programmers
use the word struct for a particularly simple class -- for example one
which doesn't inherit and which won't form a base of any inheritance
hierarchy.
That was the convention of my previous company -- the keyword struct
was used for a simple class.

Paul Epstein

Usually struct is using as a type that have a state but doesn't have
behavior. I means that there are public fields and there is no
methods. This type usually is using as a parameter of a method and/or
aggregate object. It is not a restriction but is some convention can
be adopted by developers (of the company for instance).

Andrey Bulat
 
I

Ian Collins

Andrey said:
Usually struct is using as a type that have a state but doesn't have
behavior. I means that there are public fields and there is no
methods.

In other words a C style struct.
 
I

Ian Collins

Suraj said:
C++ is backwards compatible with C struct declarations. I think
backward compatibility is the biggest reason for retaining structs.

The only difference i am aware of is that structure members have
public access by default and class members have private access by
default

Don't forget the same applies to default inheritance.
 
J

James Kanze

On Mar 21, 9:23 pm, (e-mail address removed) wrote:

[...]
Usually struct is using as a type that have a state but
doesn't have behavior. I means that there are public
fields and there is no methods. This type usually is using
as a parameter of a method and/or aggregate object. It is
not a restriction but is some convention can be adopted by
developers (of the company for instance).

As you say, it depends on the local conventions. But I've
seen quite a few struct's with member functions,
constructors, and even destructors. One very common
convention seems to be that the data members should be
either all public or all private---if they're all public,
use struct; all private, use class. I've also seen struct
used a lot for classes which have no data members (so the
above rules doesn't help much), especially traits classes.
(I'm not too hot on this convention myself, but it does seem
fairly widely used.)
 
J

Jorgen Grahn

In other words a C style struct.

Or a C style struct plus a default constructor -- the constructor has
more to do with state than with behaviour. I sometimes use structs
like that, to avoid accidents with uninitialized members. Sometimes
it's also possible to make the members const.

/Jorgen
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top