structs

Hi guys. I have a question about structs. This is an example from the site here. Outside the struct they have mine and yours. Why are they outside and why cant i put them inside the struct. Are they returning values?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// example about structures
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

struct movies_t {
  string title;
  int year;
} mine, yours;

void printmovie (movies_t movie);

int main ()
{
  string mystr;

  mine.title = "2001 A Space Odyssey";
  mine.year = 1968;

  cout << "Enter title: ";
  getline (cin,yours.title);
  cout << "Enter year: ";
  getline (cin,mystr);
  stringstream(mystr) >> yours.year;

  cout << "My favorite movie is:\n ";
  printmovie (mine);
  cout << "And yours is:\n ";
  printmovie (yours);
  return 0;
}

void printmovie (movies_t movie)
{
  cout << movie.title;
  cout << " (" << movie.year << ")\n";
}
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.
Last edited on
Topic archived. No new replies allowed.