1

I am reading Game Coding Complete and it suggests to have two init() functions, one being your usual init() call, the other taking a stream. It doesn't go into alot of detail about it though, and I am left a little confused.

class AnimatinPath
{
public:
   AnimationPath();
   Initialize(std::vector<AnimationPathPoints> const & srcPath);
   Initialize(InputStream & stream);
   // ...
};

It goes on to say you can init objects from disk, memory or over a network.

What is a stream? I've been using C++ for about 2 years and my only experience with stream is iostream. Is this suggesting I have a binary output of an object that I can use?

What would the syntax look at the other end, when creating the object.

Thanks.

  • 1
    [Don't use `init` functions](http://stackoverflow.com/q/6471136/277176), this is what constructors are for. – Yakov Galka Jun 30 '11 at 08:13
  • Thats the example from the book. I do use init functions tho, it allows me to create the objects I need and easily recycle them without having to hit the disk, creating alot of objects while the game is running causes framerate drops for me otherwise. –  Jun 30 '11 at 08:17
  • @ybungalobill 'larsmans' in that article suggests that using a factory is the exception, and since most game objects would(should?) originate from a factory it seems this is the exception. –  Jun 30 '11 at 08:32
  • 1
    @ybungalobill: That depends on several factors: 1) What subset of C++ you are using (e.g. if exceptions are disallowed, discouraged, too expensive or too buggy (been there, seen that) than any real work in constructors is impossible)? 2) What exactly you are doing in constructor (e.g. even with exceptions error handling in constructors can be problematic)? 3) How are you creating and storing the objects (e.g. objects stored in an array must use default constructor, so loading must be done elsewhere)? And so on... – Tomek Szpakowicz Jun 30 '11 at 08:36

2 Answers2

1

'Stream' is actually not important.

Second init function with a stream parameter means object serialization.

Take a look at this SO post: How do you serialize an object in C++?

You could google for 'object serialization' for more information.

Community
  • 1
  • 1
9dan
  • 4,222
  • 2
  • 29
  • 44
0

This other init is for constructing object from some stream: disc file, network data etc. In the case of AnimationPath you could have path defined in some file and use this method to load the data.

Tomek Szpakowicz
  • 14,063
  • 3
  • 33
  • 55
  • Yes Game Coding Complete by Mike McShaffry (that sounds like a made-up Scottish name) –  Jun 30 '11 at 08:21