• Create Account

## Intialization withn definition?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

8 replies to this topic

### #1gasto  Members

303
Like
0Likes
Like

Posted 17 July 2014 - 09:39 AM

  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.

Intel Core 2 Quad CPU Q6600, 2.4 GHz. 3GB RAM. ATI Radeon HD 3400.

### #2Brother Bob  Moderators

10101
Like
0Likes
Like

Posted 17 July 2014 - 09:48 AM

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.

### #3frob  Moderators

41222
Like
1Likes
Like

Posted 17 July 2014 - 10:17 AM

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, 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, 17 July 2014 - 10:27 AM.
Editor ate formatting. Multiple times. Ikcy.

Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I occasionally write about assorted stuff.

### #4Servant of the Lord  Members

33470
Like
3Likes
Like

Posted 17 July 2014 - 10:23 AM

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;
};


It's perfectly fine to abbreviate my username to 'Servant' or 'SotL' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames -

### #5gasto  Members

303
Like
0Likes
Like

Posted 17 July 2014 - 10:29 AM

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.

Intel Core 2 Quad CPU Q6600, 2.4 GHz. 3GB RAM. ATI Radeon HD 3400.

### #6belfegor  Members

2833
Like
0Likes
Like

Posted 17 July 2014 - 11:38 AM

@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)?

### #7SeanMiddleditch  Members

16704
Like
1Likes
Like

Posted 17 July 2014 - 12:31 PM

@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.

Game Developer, C++ Geek, Dragon Slayer - http://seanmiddleditch.com

C++ SG14 "Games & Low Latency" - Co-chair - public forums

Wargaming Seattle - Lead Server Engineer - We're hiring!

### #8gasto  Members

303
Like
0Likes
Like

Posted 17 July 2014 - 02:09 PM

@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?

Intel Core 2 Quad CPU Q6600, 2.4 GHz. 3GB RAM. ATI Radeon HD 3400.

### #9SeanMiddleditch  Members

16704
Like
2Likes
Like

Posted 17 July 2014 - 02:32 PM

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, 18 July 2014 - 02:25 AM.
Code tag fix up

Game Developer, C++ Geek, Dragon Slayer - http://seanmiddleditch.com

C++ SG14 "Games & Low Latency" - Co-chair - public forums

Wargaming Seattle - Lead Server Engineer - We're hiring!

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.