why use this struct format?

K

Kilana

I see this all the time in code:


typedef struct a_struct
{
...
}differentName, *differentNamePtr;


I understand how I can use it, but could someone tell me why the above is
superior to:

struct differentName
{
...
};



It seems that if you're creating the struct and giving it an alternate name
immediately (and never using it's original name), then why not just give it
the alternate name?


I also don't understand what you're saving by declaring differentNamePtr as
opposed to just using differentName *. differentName * actually seems more
clear to me when reading code.


Be as brutal as need be to get the point across, I'm ready for it.


Thanks,

-Kilana
 
K

Kilana

It's a holdover from C, when the syntax in your second example was not
permitted.

Ohhhh,

Thanks :), I've been wondering about that for years, but never asked.


-K
 
M

Mike Smith

Kilana said:
It seems that if you're creating the struct and giving it an alternate name
immediately (and never using it's original name), then why not just give it
the alternate name?

It's a holdover from C, when the syntax in your second example was not
permitted.
 
U

Unforgiven

Kilana said:
I see this all the time in code:


typedef struct a_struct
{
...
}differentName, *differentNamePtr;

It's a leftover from C. In C, giving the above definition, the following
rules apply:
a_struct a; // ERROR, not allowed in C, but allowed in C++
struct a_struct b; // That's alright in both C++ and C
differentName c; // Also good

So the typedef was in place to prevent typing 'struct' before every
definition. Of course, since 'a_struct a;' is alright in C++, this
construction is obsolete. But you still see it, either from C people that
are still in the habit, or in headers that are for both C and C++ (like the
Windows Platform SDK headers).

The same thing is true for enum.
 
G

Gregg

Mike Smith said:
It's a holdover from C, when the syntax in your second example was not
permitted.


The second example

struct a_struct {
/* ... */
}

is so permitted in C. The difference is that in C you cannot use

a_struct x;

to define a variable of this type, but instead must say

struct a_struct x;

so in C the typedefs make it more convenient to use the struct. But you are
right that the use of this in C++ is probably due to C programming habits.
 
D

Default User

Kilana said:
I see this all the time in code:

typedef struct a_struct
{
...
}differentName, *differentNamePtr;


This is required in C, but not C++. People sometimes use it to promote
cross-language compatibility, or because they don't know better.
I understand how I can use it, but could someone tell me why the above is
superior to:

struct differentName
{
...
};

It's not. Use this form for C++. However, you should really ask why you
are using a struct at all. In most cases, it is recommended to use
classes only.



Brian Rodenborn
 
W

White Wolf

Default said:
However, you should really ask why
you are using a struct at all. In most cases, it is recommended to use
classes only.

structs *are* classes. The *only* difference is their default protection
level. struct members are public by default and structs inherit other class
types public by default. This is the _only_ difference. Please do not
spread fals information here. The use of the class keyword over the struct
keyword is only a matter of style or preference. The language has
absolutely no preference for one over the other:

class foo {
// private elements
public:
// public interface
};


Some prefer (eg: for easy reading) writing it as:

struct foo {
// public interface first, no scrolling required
private:
// internals
};

Some write it like

class foo {
public:
// public interface first, no scrolling required
private:
// internals
};

The only - sort of - argument to support the 3rd form is to avoid confusion
of those (like you) who think struct is not a class type.
 
D

Default User

White said:
structs *are* classes.

No kidding.
The *only* difference is their default protection
Really?

struct members are public by default and structs inherit other class
types public by default.

No kidding.
This is the _only_ difference.

Whoop-de-do. It's a BIG difference.
Please do not
spread fals information here.

I'm not spreading false information, I'm expressing an opinion. Newbies
should not be using structs, with their default public behavior. They
should be using classes, with default private, so they learn proper
handling of member data. The tendency for newbies is to use structs just
like C-style ones, with direct access to the attributes. That's not the
way they should be starting out.
The only - sort of - argument to support the 3rd form is to avoid confusion
of those (like you) who think struct is not a class type.

It's amazing how you can determine what I do and do not know. You happen
to be extraordinarily wrong.

Just admit you're still crying about that previous thread. You saw what
you thought was an opportunity to show me up. Well, you was wrong bucko.



Brian Rodenborn
 
W

White Wolf

Default said:
Whoop-de-do. It's a BIG difference.

No, it is not. Bjarne Stroustrup, The C++ Programming Language Spec. Ed.:

"By definition a struct is a class..."

Breaking here. Your sentences (see above) suggest to the poor newbie that
structs are *different* from classes. This is false information. The
creator of the language says otherwise:

"
By definition a struct is a class in which members are by default public;
that is,

struct s { ...

is simply a shorthand for

class s { public: ...
"

I'm not spreading false information, I'm expressing an opinion.

You are spreading the false information that classes are preferred over
struct. Struct *is* a class. Did you say "the use of the class keyword is
preferred over the use of the struct keyword when defining a class type in
C++" that would have been a completely different statement. And it would
even become "expressing of an opinion" had it been starting with IMHO, IMO,
"I think", "My opinion is" etc.
Newbies should not be using structs, with their default public
behavior.

You think...
They should be using classes, with default private,

You are still confused: structs are classes. You mean they should use the
class keyword for their class declarations instead of the struct keyword.
so they learn proper handling of member data.

I see. I do not believe that it will in any way help in that but I can
accept your opinion. (I do not believe because I have seen many people to
use C++, they did not know that they can use struct to define classes and
still all their members were public.)
The tendency for newbies is to use structs
just like C-style ones, with direct access
to the attributes.

You mean newbies coming from C. Since newbies using C++ will learn about
classes and struct and on the same page of a decent book they will learn
about not to make everything public.
That's not the way they should be starting out.

I have just too much bad experiences to say that it has nothing to do with
using the struct or the class keyword.
It's amazing how you can determine what I do and do not know. You
happen to be extraordinarily wrong.

Even in your reply you say _again_:
"Newbies should not be using structs... They should be using classes"

So what can you assume would this statement? That you are not aware of the
fact that types defined by the struct keyword *are* classes. Since you say
that instead of them newbies have to use classes. But they *are* classes.
Just admit you're still crying about that previous thread.
You saw what you thought was an opportunity to show me up.
Well, you was wrong bucko.

You should really need to that medication. You are getting worse.

I do not know why do you think that you are a bit important for me to
remember you or any thread of yours. When I reply in a thread I look at the
content of the post and not the poster. I suggest you start doing the same.

BTW start to get used to the fact that you are not that important or
extraordinary that people will just reply to your posts because you posted
them. At least I don't.
 
D

Default User

White said:
No, it is not. Bjarne Stroustrup, The C++ Programming Language Spec. Ed.:

"By definition a struct is a class..."

You are really starting to piss me off. First you say there is a
difference, then you say there isn't. I KNOW the difference between
structs and classes. I also know why they made it into C++.

The default public behavior is a significant behavioral difference, and
one that I think promotes poor programming practices for newbies.
Period. I think. My beliefs. You may not share that belief, but it
doesn't make it incorrect.
Breaking here. Your sentences (see above) suggest to the poor newbie that
structs are *different* from classes. This is false information. The
creator of the language says otherwise:

Which you also said. I agree with you on the difference. Here's what you
said, "This is the _only_ difference". So there is a difference.

You are spreading the false information that classes are preferred over
struct.

They are prefered. Prefered by me. Are you dim to understand that? Oh
no, I got personal! WW will now bitch and moan and complain.

You think...

Indeed I do. You should try it.


Brian Rodenborn
 
W

White Wolf

Default said:
You are really starting to piss me off. First you say there is a
difference, then you say there isn't. I KNOW the difference between
structs and classes. I also know why they made it into C++.

Right. The you also know that your statement that structs are not classes
is plain wrong. That was the statement I debated.
The default public behavior is a significant behavioral difference,
and
one that I think promotes poor programming practices for newbies.
Period. I think. My beliefs. You may not share that belief, but it
doesn't make it incorrect.

I was not trying to change your opinion I was only telling you mine.
Which you also said. I agree with you on the difference. Here's what
you said, "This is the _only_ difference". So there is a difference.

struct are classes. Period. In the standard, in TC++PL etc.
They are prefered. Prefered by me. Are you dim to understand that? Oh
no, I got personal! WW will now bitch and moan and complain.

How can you prefer classes over struct when struct *is* a class? Use may
prefer using the class keyword to define your classes over the struct
keyword.
Indeed I do. You should try it.

I do think. Logic is what proves your statement misleading for newbies
(stating that struct are not class types) and the standard is there to prove
you wrong. Case closed by me.

It makes absolutely no sense to debate technical issues with you, since you
twist the words in order make up a non-existing personal attack on you.
 
E

E. Robert Tisdale

Kilana said:
I see this all the time in code:

typedef struct a_struct {
// ...
} differentName, *differentNamePtr;

I understand how I can use it
but could someone tell me why the above is superior to:

struct differentName {
// ...
};

It seems that if you're creating the struct and giving it an alternate name
immediately (and never using it's original name),
then why not just give it the alternate name?

I also don't understand what you're saving
by declaring differentNamePtr as opposed to just using differentName*.
differentName* actually seems more clear to me when reading code.

I agree.
Be as brutal as need be to get the point across, I'm ready for it.

The "struct" keyword in C++ is not useful
except for backward compatibility with C.
You should use the "class" keyword instead
unless you need to compile the struct definition with a C compiler.
I do it like this:

#ifdef __cplusplus
#define PRIVATE private:
#else /* __cplusplus */
#define PRIVATE
#endif /* __cplusplus */

typedef struct a_struct {
PRIVATE
// data members
} a_struct;

#undef PRIVATE

This typedef allows C programmers to write

a_struct an_instance;

instead of the more clumsy

struct a_struct an_instance;
 
M

Mike Wahler

White Wolf said:
Default User wrote:

I do think. Logic

Here, you appeal to logic.
is what proves your statement misleading for newbies
(stating that struct are not class types) and the standard is there to prove
you wrong. Case closed by me.

It makes absolutely no sense to debate technical issues with you,

So why do you do it? If this debate makes 'no sense'
then it's illogical to continue, isn't it? :)


-Mike
 
W

White Wolf

Mike said:
So why do you do it? If this debate makes 'no sense'
then it's illogical to continue, isn't it? :)

Which part of "Case closed by me." you do not understand?
 
W

White Wolf

E. Robert Tisdale said:
The "struct" keyword in C++ is not useful
except for backward compatibility with C.

How about its usage in the standard library? How about the fact that

struct T { ...

is a synonim for:

class T { public:...

?

There is absolutely no difference between:

struct T {
// Public interface
protected:
// Class family interface
private:
// Private stuff
};

And
class T {
public:
// Public interface
protected:
// Class family interface
private:
// Private stuff
};

And one might prefer using the struct keyword to keep the public interface
at the beginning of the class declaration (yes, struct declares a class).
It is a matter of personal taste.

I believe the problem to aim at is improper encapsulation. The above class
declaration using the struct keyword is far superior (if the public
interface is proper) to what most newbies do:

class T { // Use of class keyword
int i;
public:
int getInt() const;
void setInt( int j);
};
 
R

Ron Natalie

E. Robert Tisdale said:
The "struct" keyword in C++ is not useful
except for backward compatibility with C.

Well it saves some typing in the case where you're defining things that
don't have any private parts.
 
E

E. Robert Tisdale

Ron said:
Well it saves some typing
in the case where you're defining things
that don't have any private parts.

You mean, "A struct as a neutered class." ;-)
 
W

White Wolf

Ron said:
Well it saves some typing in the case where you're defining things
that don't have any private parts.

Poor things. What fun can they have on a Saturday night? ;-)
 
D

Default User

White said:
Right. The you also know that your statement that structs are not classes
is plain wrong. That was the statement I debated.


Go troll somebody else.




Brian Rodenborn
 

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
474,142
Messages
2,570,819
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top