Quantcast
Channel: Struct Question
Viewing all articles
Browse latest Browse all 3

Struct Question

$
0
0
Regarding #1:

Back in the C days you declared a struct like this:
struct Foo {
   int x;
};


then you declare a variable like this:
struct tagFoo foo1;

since people hated typing the "struct" keyword, they often did a typedef to shorten the syntax so that the struct keyword wasn't required.
typedef struct Foo FooShortcut;

then you declare a variable like this:
FooShortcut foo2;

Then people started combining the structure declaration with the typedef shortcut into a single statement like this:
typedef struct Foo {
   int x;
} FooShortcut;


and declare the variable as before, like this:
FooShortcut foo3;

Then then you can call the original struct something else and treat the shortcut/typedef as the canonical name.
typedef struct tagFoo {
   int x;
} Foo;


and declare the variable with the previous short syntax, but with an even shorter name:
Foo foo4;


in C++ you can omit the struct keyword when declaring the variable in the first place:

struct Foo
{
      int x;
};
Foo foo5;


Regarding #2

struct Foo
{
   int x;
   bool EqualTo( const Foo& other ) const
   {
      return x == other.x;
   }
};

But you should consider writing it as an == operator.

struct Foo
{
   int x;
   bool operator==( const Foo& other ) const
   {
       return x == other.x;
   }
};

Viewing all articles
Browse latest Browse all 3

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>