• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
gasto

Intialization withn definition?

8 posts in this topic

  struct Tile
  {
    TileType      Type;
    unsigned int  Color;
    unsigned int  Flags;

    Tile( TileType Type = TILE_EMPTY, unsigned int Color = 0xffffffff, unsigned int Flags = 0 ) :
      Type( Type ),
      Color( Color ),
      Flags( Flags )
    {
    }
  };

 

Can somebody explain to me what on earth is the coder doing here?

a struct is being initialized with default parameters within its own definition. I fail to assimilate its semantics and usage.

0

Share this post


Link to post
Share on other sites

The constructor is just a a regular function and has default parameters. The initializer list is used to initialize the members of the class using the parameters to the constructor. If you are confused by the fact that the members and the parameters have identical names, then that is no problem (using the Type member as an example):

Type(Type)

The first Type is the member to be initialized, and the second Type is the value to initialize it with. Only members can be initialized, to the first Type cannot reference anything other than the symbol that is a member of the class. The second Type follows normal name resolution rules for overloaded names: function parameters take priority over members, so the second Type resolves to the parameter to the constructor and not the member.

0

Share this post


Link to post
Share on other sites

Tile( TileType Type = TILE_EMPTY, unsigned int Color = 0xffffffff, unsigned int Flags = 0 ) :
... I fail to assimilate its semantics and usage.
Usage, you can use any of these:

Tile t();
Tile t(TILE_WHATEVER)
Tile t(TILE_WHATEVER, 0xbaadf00d);
Tile t(TILE_WHATEVER, 0xbaadf00d, flags);

Default parameters occasionally trip up programmers over some compiler rules, and they sometimes trip up long-term growth of released APIs. It may not be a problem with a pristine code base, but as the code grows and new functions are introduced and unexpected patterns arise and all the little useful-but-ugly growths start to naturally appear, default parameters invariably end up with collisions and headaches that could have been avoided.

In the long term it is better to explicitly provide a family of functions with all the desired signatures.

    Tile(  ) ...
    Tile( TileType Type )  ...
    Tile( TileType Type, unsigned int Color) ...
    Tile( TileType Type, unsigned int Color, unsigned int Flags)  ...
Providing all the signatures is much better for long term maintenance. Edited by frob
Editor ate formatting. Multiple times. Ikcy.
1

Share this post


Link to post
Share on other sites

 

In C++11, default-initializing your member-variables becomes much clearer:

struct Tile
{
    TileType      Type  = TILE_EMPTY;
    unsigned int  Color = 0xffffffff;
    unsigned int  Flags = 0;
};

This makes much more sense and is less tautological.

0

Share this post


Link to post
Share on other sites

@Servant

How is that thing called so i can google?

 

When that initialization takes place? Does the object needs to be fully constructed (say i want to use that variable in constructor body)?

0

Share this post


Link to post
Share on other sites

@Servant
How is that thing called so i can google?


Non-Static Data Member Initializes, often abbreviated NSDMI.

When that initialization takes place? Does the object needs to be fully constructed (say i want to use that variable in constructor body)?


It's just syntactic sugar. These two pieces of code are functionally equivalent:

struct foo {
  int i = 0;
};

struct foo {
  int i;
  foo() : i(0) {}
};
The advantage to NSDMI shines when you have only a default constructor (easier to read) or when you have many constructors (since you don't need to repeat the initializers over and over again.

Note that these are _not_ equivalent to the original code that was posted, as the original code allows the user of the class to supply non-default data to the constructor. The above two examples both only supply a default constructor (no parameters). You can use NSDMI with the original example by doing something like:

struct foo {
  int i = 0;
  foo() = default;
  foo(int i) : i(i) {}
};
Whether you find that an improvement or not is another question.
1

Share this post


Link to post
Share on other sites

 

@Servant
How is that thing called so i can google?


Non-Static Data Member Initializes, often abbreviated NSDMI.

When that initialization takes place? Does the object needs to be fully constructed (say i want to use that variable in constructor body)?


It's just syntactic sugar. These two pieces of code are functionally equivalent:

struct foo {
  int i = 0;
};

struct foo {
  int i;
  foo() : i(0) {}
};
The advantage to NSDMI shines when you have only a default constructor (easier to read) or when you have many constructors (since you don't need to repeat the initializers over and over again.

Note that these are _not_ equivalent to the original code that was posted, as the original code allows the user of the class to supply non-default data to the constructor. The above two examples both only supply a default constructor (no parameters). You can use NSDMI with the original example by doing something like:

struct foo {
  int i = 0;
  foo() = default;
  foo(int i) : i(i) {}
};
Whether you find that an improvement or not is another question.

 

What does foo()=default; do?

0

Share this post


Link to post
Share on other sites

What does foo()=default; do?


It's a defualted function. It tells the compiler to generate the function definition as it normally would if that function were to be defined. When you overload any constructor, C++ ellides the default constructor by default (though not the copy constructor). If you want a default constuctor, you can use =default. This has a few important differences with definining an empty default constructor wrt triviality (a new type property in C++11), eg.
 
// no default constructor
struct foo {
  int i;
  foo(int i) : i(i) {}
};

// trivial default constructor
struct foo {
  int i;
  foo() = default;
  foo(int i) : i(i) {}
};

// non-trivial default constructor
struct foo {
  int i = 0;
  foo() = default;
  foo(int i) : i(i) {}
};
Being trivial means there's no actual code generated. The constructor doesn't actually do anything - it doesn't set any member variables, it does nothing at all, and so optimizations can be applied that ellide the call to the default constructor entirely. The first example doesn't define a default constructor but does define another constructor, so there will be no default constructor (you must supply the argument). The third case requires the default constructor to initialize i to 0, so there is code that must run, and so it is not trivial.

You can use =default with default constructors, copy constructors and assignment operators, and move constructors and move assignment operators. You can also use =delete in order to tell the compiler not to generate a function at all even if it normally would (and this cna be used for overload handling). e.g., to make a type non-copyable in C++11, do:

struct foo {
  foo(foo const&) = delete;
  foo& operator=(foo const&) = delete;
};
That code just means that there is no copy constructor or assignment operator and any attempts to invoke them (either implicitly or explicitly) will fail at compile time (with a far better and more helpful message than you'd get with the old C++98 private copy constructor trick). You can also use it for overloading for tricky overload sets, templates, etc. Edited by phantom
Code tag fix up
2

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0