• 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
Tibetan Sand Fox

Is that way of using constructor really better ?

29 posts in this topic

Hello.
Not so long ago I started to learn D3D11 and at the moment I'm swimming through "Beginning DirectX 11 Game Programming" book. I found one thing, which I'm curious about. Author said that constructor’s member initialize list can be more efficient and is better programming practice than letting a constructor do the job in default way.

My question is as in topic, is that way of initializing members really better ?

Example from book:

[CODE]D3DBase::D3DBase() : driverType_( D3D_DRIVER_TYPE_NULL ), featureLevel_( D3D_FEATURE_LEVEL_11_0 ),
d3dDevice_( 0 ), d3dContext_( 0 ), swapChain_( 0 ), backBufferTarget_( 0 )[/CODE]

Thank you in advance for any help. Edited by Agbahlok
0

Share this post


Link to post
Share on other sites
[edit: I was waaaay off on my original comment]

@Servant of the Lord: you should prefix literals with an underscore, as literal suffixes not starting with an underscore are reserved for future use. But thanks for reminding me that they exist! Edited by Cornstalks
2

Share this post


Link to post
Share on other sites
Most modern compilers will optimize the trivial stuff. So I only stick important things (large items, parent classes, references, ect...) in the initializer list, as I really don't like them. My big issue is that the order items are initlialized in the initializer list is not the same order as they are listed. Which for dependent items can really lead to some hard to track down bugs.
-2

Share this post


Link to post
Share on other sites
[quote name='Ryan_001' timestamp='1349347882' post='4986714']
My big issue is that the order items are initlialized in the initializer list is not the same order as they are listed. Which for dependent items can really lead to some hard to track down bugs.
[/quote]

Ah, but should initialisation be in the initialiser list order, or the order you declared them in the class declaration?

FWIW many coding standards suggest keeping your initialiser lists in the same order as your member declarations for exactly this reason.
2

Share this post


Link to post
Share on other sites
[quote name='mrbastard' timestamp='1349349835' post='4986723']
[quote name='Ryan_001' timestamp='1349347882' post='4986714']
My big issue is that the order items are initlialized in the initializer list is not the same order as they are listed. Which for dependent items can really lead to some hard to track down bugs.
[/quote]

Ah, but should initialisation be in the initialiser list order, or the order you declared them in the class declaration?

FWIW many coding standards suggest keeping your initialiser lists in the same order as your member declarations for exactly this reason.
[/quote]

Their order in the class definition is either for readability/clarity, or compactness/cache coherency. No one orders their variables in intialization order, at least, no one outside of C++. Not to mention dependencies can change based on the constructor that is chosen. If the order of initialization occurs in order of the intialization list, then at least you have some control in regards to dependencies. Course IMO the best solution is just to use intializtion lists when absolutely necessary, use standard C++ code the rest of the time.
0

Share this post


Link to post
Share on other sites
One gotcha though related to initializer lists is that exception safety is not possible if allocating memory to raw pointers.

Consider
[code]
Bar::Bar()
{
throw SomeException();
}

class Foo
{
int *somePointer;
Bar bar;
};

Foo::Foo()
:somePointer(new int[10])
{
}

Foo::~Foo()
{
delete[] somePointer;
}
[/code]

The above code has a memory leak, somePointer is never freed since construction of Bar throws an exception. Edited by clb
0

Share this post


Link to post
Share on other sites
[quote name='Bregma' timestamp='1349356831' post='4986749']
If there is a compiler that does not issue a warning when you put initializers out of order, you should switch to one that does.
[/quote]

FYI Visual c++ 2012 does not issue a warning about this with /wall (and 'language extensions' disabled fwiw). I know gcc does, and I agree that it's a useful warning.
2

Share this post


Link to post
Share on other sites
[quote name='Servant of the Lord' timestamp='1349319567' post='4986638']
[code]
int height = 5ft + 9in;
int weight = 160lbs;
[/code]
[/quote]
O_o, units!? That would be absolutely awesome. Is this some c++11 thing? How does that work? Ref? (Google gave nothing)
0

Share this post


Link to post
Share on other sites
Yep, they are a C++11 feature. Suffixes only, no prefixes at this point. Here's [url="http://en.wikipedia.org/wiki/C%2B%2B11#User-defined_literals"]wikipedia's thoughts[/url].
Alas I can't use it yet, since I'm still on MinGW/GCC 4.6, and those (and most of the above C++11 features I mentioned) are on 4.7. [img]http://public.gamedev.net//public/style_emoticons/default/sad.png[/img]

@Cornstalks: Thanks for the tips about the underscore! The user-defined literals should've also (better/safer coding) returned a user-defined type, like Length, instead of putting it into a generic int: [i]Length myHeight = (4_feet + 8_inches);[/i] Edited by Servant of the Lord
0

Share this post


Link to post
Share on other sites
Thanks a lot for all those MUCH useful answers ! I didn't even expect to learn so much when I posted this thread. I know everything I wanted to know and even more. Well, I gave most of you +rep. Also you convinced me to use initializer lists more often. It's just drop in the ocean of knowledge I'd like to have, but, well, that's a step. Thank you.
0

Share this post


Link to post
Share on other sites
[quote name='taz0010' timestamp='1349502023' post='4987326']
Personally if I have primitive variables that I need to initialise in a specific order, I'm not going to rely on the class definition order to do it, and that means good old fashioned assignment with the = operator.
[/quote]
And if I need bananas, I'm going to eat good old fashioned apples!

assignment != initialization [img]http://public.gamedev.net//public/style_emoticons/default/wink.png[/img]

By the time your assignment is actually reached, your variables will have already been default-initialized (though it'd [i]probably[/i] be optimized out* if the variable isn't accessed before your assignment).

*[size=2]Which is less a guarantee than the standard-guaranteed initialization order.[/size]

Why wouldn't you rely on the standardized initialization order? The only reason I can think of, is third-party APIs that have weird initialization requirements, requiring you to be 100% sure the API is initialized before you can, for example, load resources. This forces you to break out some initialization outside of the constructor entirely, into separate Init() functions, mucking up RAII for any globally-constructed variables.
But even in that situation, your assignments would still need to happen completely out of the constructor, and it wouldn't be a matter between constructor-body or initializer list, but between the entire construction process or a separate member-function called later.
0

Share this post


Link to post
Share on other sites
[quote name='taz0010' timestamp='1349502023' post='4987326']
Why is this being voted down? His gripe about the initialiser list order not being the actual initialisation order is perfectly valid. It's intuitive to assume that the variables are going to be initialised in the order that the initialisation code is written - which is exactly the case whenever you initialise something using the equals sign.[/quote]

The only time the equals sign has anything to do with initialization is if it is done at the same time as the declaration. So it is [b]still[/b] the declaration order that matters. Or to point it out very clearly
[code]
int x;
x = 10; //This is NOT initialization

int x = 10; //THIS is initialization... also technically a special case syntax for "int x(10);"
[/code]

[quote]
Also mentioned in this topic, ordering your members for alignment or caching reasons can potentially break your code if one member was initialised using the value of another.[/quote]

Which would seem like a very good reason not to do that, instead of saying "I'll simply not do proper initialization, so I can make something work, that shouldn't be done in the first place." How many times do you have a situation, where it's not just as easy to initialize both members by the same parameter/default value? And if one value is calculated from the other, how likely is it that one of those isn't redundant and it is vital for your performance to precalculate them both? Edited by Trienco
0

Share this post


Link to post
Share on other sites
[quote name='Hodgman' timestamp='1349525505' post='4987371']
I don't know the background behind why the C++ standardization people decided to enforce initialization in declaration order, instead of initialization in initialization order, but IMHO it was a mistake to make that decisiion. Initialization order is bound by logical constraints, but declaration order should only be a concern for memory layout, and should be decoupled from the concern of initialization.
[/quote]
IIRC, it's so that member variables would satisfy the last constructed, first destroyed order that is the general case for variables whose lifetime are managed by the compiler. In theory, the padding/member variable order issue was addressed by allowing the compiler to reorder position of member variables with different access specifiers, but in practice compilers don't actually take advantage of that.
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