0

let's say i want to create a movie database software.

i need a class Actor that will have attribute as name, surname, dob, awards, and it should also have an array of "movie" object.

i also need the class Movie, that will have attribute as title, duration, year, but it should also have an array of "actor" object. to represent the cast of the movie.

Actor object need a list of all the movie done by that actor, just as the movie object need a list of all the actors that played in it. that gets quite messy cause it's a class containing another one and viceversa, the compiler get stuck.

which would be the right way to represent this situation? 
Martin Magakian
  • 3,746
  • 5
  • 37
  • 53
mystudioos
  • 99
  • 1
  • 1
  • 8
  • 4
    The solutions depends on the language. So please pick one language. – Some programmer dude Jul 23 '13 at 09:15
  • 2
    Since this is almost certainly homework (no way you can "beat" IMDB anyway), you probably want to approach it in the way of "Show your thinking" rather than "Ask for our suggestions". (And no, it makes no difference if you came up with the project on your own and you are your own teacher - the main part of learning programming is to learn how to come up with solutions to problems such as this) – Mats Petersson Jul 23 '13 at 09:23
  • Looks like OP could use a few pointers on this one. – High Performance Mark Jul 23 '13 at 09:23
  • 1
    @HighPerformanceMark Is that a subtle hint? Or did you mean to just vector our advice? – Mats Petersson Jul 23 '13 at 09:24
  • Just have an `Actor` object containing a `List` of `Movie` objects related to the actor and vice versa. For database storage, just have additional relational tables that show how they are linked. I do something like this in my current project. – Mark M Jul 23 '13 at 09:31
  • @MarkM: that's exactly the model that the question proposed, then says "it's a class containing another one and viceversa, the compiler get stuck". How to allow that is obviously so obvious to you that you don't even bother to mention it for a non-database implementation ;-). – Tony Delroy Jul 23 '13 at 09:35
  • @TonyD Well, he did ask the right way to represent the situation...and yeah, the 'compiler got stuck' part is confusing without an actual error message or stacktrace. – Mark M Jul 23 '13 at 09:40
  • If the language is Java, this is called a mutual dependency, and this might be helpful: http://stackoverflow.com/questions/3646113/circular-dependency-in-java-classes – Fabinout Jul 23 '13 at 09:43
  • @MarkM: I think it must mean that `class Movie { list actors; ... }; class Actor { list movies; };` can't be populated (if the objects are stored independently and not somehow implicitly references to shared instances in the language being used) because the cylic dependency needs infinite memory even for 1 actor / 1 movie. – Tony Delroy Jul 23 '13 at 09:51

2 Answers2

1

You can use references, Actor* in class Movieand Movie* in class Actor. And forward declarations of the classes:

class Actor;

public class Movie { ... Actor* ... }

This in the header. In the class implementation, .cpp, you then might include the header of the other class, and use methods of the other class.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
0

In most languages the concept is the same. As you've observed, you can't have "lists of Movie objects" embedded in Actors and vice versa, so instead keep a list of each and just "reference" the other objects in some way. This might be using some unique reference value (e.g. an incrementing number) that can be used to search the other list or associative container (e.g. use a key/value "std::map" in C++ rather than a linked list), or by keeping a reference or (weak) pointer directly to the logically linked objects....

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
  • i'm just exercising. using objective-c. my problem is that i don't want to use databases, or i would just give both classes some id n link them in a table. so any idea for my exercise? not homework... just selftraining – mystudioos Jul 23 '13 at 10:05
  • that's something i tought about. to give both class an ID, and create an array or a nsdictionary ( hash map ) to match the values... but wasn't sure was the right or best solution – mystudioos Jul 23 '13 at 10:15
  • I'm wasn't imagining you using a database, but even for an in-memory implementation some way to refer to the other type of object without actually keeping it by value is needed. Often, you can e.g. store the objects in a vector, hash or tree, then have multiple additional hash tables, maps or multimaps (which map each key to any number of records) resolving different key fields (e.g. movie name, director, year). I don't know objective-c at all though - hard to give example code. – Tony Delroy Jul 23 '13 at 10:33
  • i'm gonna try with an array or an ash table and i'll let you know how's going :) thanks everyone in the meanwhile :D – mystudioos Jul 23 '13 at 12:13