Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

In general, should I use some sort of convention for structure names which is distinct from other type name? I was thinking about this when my professor started talking about structures.

I had the following discussion with myself, and since I do not want to come to any unreliable conclusions. How would you answer the question(s) and which answer(s) are untrustworthy?

Q: Should I distinguish structure names and type names to make the code more clear?

A: A structure is a type, so there doesn't need to be a distinction.

RE: Q: So there is never a case when someone uses typedef int SOMETYPE to rename a built-in type to make the code more flexible?

RE: A: There is no practical reason to rename built-in types. Actually, defining int types as SOMETYPE makes the code less readable because someone reading the code will not know the built-in type used for that type.

Honestly I do not trust either answer. Looking at some of the base structure in a new Win32Project using Visual Studio, I see things like typedef short HFILE; which, I guess, makes it more clear to the programmer what the variable is used for. Perhaps that is too subjective however.

As an aside, how does one usually determine what is going on when they come across variable declarations which use types you have never seen before. Do you just rely on the IDE to take you to or display the declaration?

share|improve this question
2  
The reasons behind so many Windows data types are explained here - stackoverflow.com/questions/5144507/…. In short- "1)they're historical types preserved for backward compatibility 2)they're different names for the same type that arose from different teams of developers." –  SChepurin Feb 26 '13 at 7:33
    
Standard library's convention is macros all uppercase, underscore separated words and everything else all lowercase, underscore separated words, no prefixes for anything. –  Jan Hudec Feb 26 '13 at 9:20
    
"There is no practical reason to rename built-in types". Seriously? What about multi-platform? –  Manoj R Feb 26 '13 at 10:05
add comment

closed as not a real question by BЈовић, Kilian Foth, Martijn Pieters, ChrisF Feb 26 '13 at 10:48

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

up vote 3 down vote accepted

In C++ structs and classes are equivalent, except that the default visibility for struct members is public, while for classes it is private. The following are equivalent:

struct X { private: int i; }

and

class X { int i; }

There is no reason to give structs a special prefix.

Also, just because something is in the Win32 API doesn't make it a good idea

Perhaps typedef short HFILE was useful for a C API, in case they later wanted to change to typedef int HFILE. It's a poor practice for C++, where you could write:

struct HFILE { short s; }

This would allow passing HFILEs around by value as efficiently as shorts, while preventing unwanted conversion between an HFILE and numeric types.

share|improve this answer
    
That brings up a good point. Wouldn't it be better instead of writing typedef short HFILE you could do #define HFILE short? And I am glad you mention that HFILE can be converted undesirably, that is a useful pitfall to be aware of. Are you also then implying that a structure will never be converted. And I am having trouble finding an example code where HFILE will be converted. –  Leonardo Feb 26 '13 at 5:02
1  
@Leonardo: Avoid #define in C++ code. Use typedef or using for types, anonymous enums for numeric constants, templates and inline functions for expressions. –  Jan Hudec Feb 26 '13 at 9:22
    
@Leonardo: Structure can't be converted except by explicitly provided conversion constructors and operators; you only provide the ones that really, really make sense, which is usually none. –  Jan Hudec Feb 26 '13 at 9:24
4  
I would go even further and say that most conventions used in Win32 API are rather bad ideas. –  Jan Hudec Feb 26 '13 at 9:30
    
@Jan: I believe your opinion is shared by the majority of expert C/C++ programmers with experience beyond Windows. Even given Microsoft's backward compatibility problems, it's hard to see why the Win32 API is so ugly compared to POSIX. –  kevin cline Feb 26 '13 at 18:10
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.