mine and yours are struct instances, these are the actual objects whose type is
movies_t
each mine and yours have it's very own members title and year, they do not share the same title and year.
the only thing they share is a type (moveis_t) which means both mine are yours are the same "family"
this means that
movies_t
is a new type, it's a type the same way as an
int
or
double
is, except that struct types can have members and functions while
int
's can't.
are you still guesing why you can't put them inside a struct?
1 2 3 4
|
struct movies_t {
string title;
int year;
} mine, yours;
|
if we put them inside the struct then there will be no struct instance and you'll have to create an instance on your own:
1 2 3 4 5
|
struct movies_t {
string title;
int year;
int mine, yours; // we put them into the cage now
};
|
note that the above struct has 4 members but 0 instances (the actual objects)
now let's see if that will work..
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
int main()
{
// ...
printmovie (mine); // now what? there is no movie named "mine" anymore.
cout << "And yours is:\n ";
printmovie (yours); // the same here
// create a movie
movies_t new_movie;
new_movie.title = "some movie"
printmovie(new_movie); // ok
return 0;
}
|
we could also remove them completely:
1 2 3 4
|
struct movies_t {
string title;
int year;
}
|
nothing different except that struct has only 2 struct members but the struct is still not created,
you can create one or more in two ways:
movies_t instance;
or
put instance before
;
as it was originaly
1 2 3 4
|
struct movies_t {
string title;
int year;
} instance;
|
now you can use the instance of this struct:
instance.title = "bruce lee";
what's the difference?
an instance created before
;
is globaly accessible to all functions located after the struct declaration:
struct declaration:
1 2 3 4 5 6 7 8 9
|
struct movies_t {
string title;
int year;
} instance;
// each of these function can access the instance object
void f1() {instance;}
void f2() {instance;}
void f3() {instance;}
|
however here is different
1 2 3 4 5 6 7 8
|
struct movies_t {
string title;
int year;
};
void f1() {}
void f2() { movies_t instance;} // the instance now exist only here
void f3() {}
|
struct declarations are just that, they specifiy what an instance of the struct have and what that instance can do.
in the above examples the instance does nothing special, it only holds movie data.