On Mon, 10 Mar 2008 21:26:12 -0800, Jim Johnson <ao*******@yahoo.com>
wrote in comp.lang.c++:
So there are 2 ways to declare a struct
1) C way of declaring
No, this is not declaring anything, this is defining a type, and also
creating an alias for it.
typedef struct {
.... etc
} SetupRecord;
This is also perfectly valid C++. It defines a structure type with no
name, and defines SetupRecord as an alias for that type. This is
actually not a particularly good idea in either language.
2) C++ way of declaring a struct
Again, your sample below does not declare a struct, it defines a
struct type.
struct SetupRecord {
.... etc
} ;
This is perfectly valid C does as well. In both languages, it defines
a structure type named SetupRecord. In either language, the type can
be referred to by the two-word phrase "struct SetupRecord". In C++
only, it can also be referred to merely as SetupRecord.
may I correct?
and also the following is incorrect synatx to declare a struct
Your terminology is still incorrect. Your snippet below defines a
structure type, and also defines an object of that structure type.
struct {
.... etc
} SetupRecord;
You have defined an unnamed structure type, and an instance of that
type named SetupRecord. In this situation, SetupRecord is the name of
a single object of this type, and not of the structure type itself.
It is perfectly valid code, and this object can be accessed anywhere
within its scope. Compile and execute the following example:
#include <iostream>
struct { int x; int y; } ss;
void show_me()
{
std::cout << ss.x << " + " << ss.y << " = "
<< (ss.x + ss.y) << std::endl;
}
int main()
{
ss.x = 2;
ss.y = 3;
show_me();
return 0;
}
--
Jack Klein
Home:
http://JK-Technology.Com
FAQs for
comp.lang.c
http://c-faq.com/
comp.lang.c++
http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html