• 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.
  • entries
    33
  • comments
    37
  • views
    38366

[Structure][C++11] The Auto Variable

Sign in to follow this  
Followers 0
superman3275

1422 views

How many C++ programmers reading this know what an auto variable is. Come on, raise your hand. I count 1... 2... 3... Almost all of you!

However for those of you that don't know the basics of the new standard yet, auto is a new variable that works like this:


Code:


// Hooded.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
auto Compiler_Determines_This_Is_An_Int = 5;
auto Compiler_Determines_This_Is_An_String = "I Am A Auto-String";
auto Compiler_Determines_This_Is_A_Float = 3.14;
auto Compiler_Determines_This_Is_A_Char = 'C';
auto Compiler_Determines_This_Is_A_Boolean = true;
std::cout << Compiler_Determines_This_Is_An_Int << std::endl;
std::cout << Compiler_Determines_This_Is_An_String << std::endl;
std::cout << Compiler_Determines_This_Is_A_Float << std::endl;
std::cout << Compiler_Determines_This_Is_A_Char << std::endl;
std::cout << Compiler_Determines_This_Is_A_Boolean << std::endl;
system("pause");
return 0;
}

Output:

5
I Am A Auto-String
3.14
C
1
Press any key to continue . . .



I can't believe it! It's the equivalent of a "var" in other, newer programming languages! It's perfect!

So, what just happened? The compiler used the values I initialized the variables with too set their type. Essentially, an auto variable that's type can be determined by the compiler.


There are some rules, however:

  • Auto variables have to be initialized to a value when they're created.
  • Auto variables can't have their types changed after initialization.

    There are many uses for this in advanced and generic programming, however I'm going to talk about it's use for the beginning C++ programmer.

    First off, I believe that using this for generic programming is fine, as clarified by Aardvajk.

    The problem arises when beginning programmers want to be lazy and use these for everything. That is bad programming practice, and a lazy work ethic. You should know the types of your variables in your program, and using the auto variable because you don't want to memorize the names of the basic types defeats the purpose.

    Now, where can I see myself using this variable? When I don't know the type of a variable I'm receiving. This generally doesn't happen very often unless you're dealing with generic programming, in which using this value is perfectly fine.

    I also believe auto variables could be very confusing. If someone defines all their variables as auto variables, than I'll have no idea what the type of each variable is. This is especially noticeable in classes with an auto variable in them. If the variable is initialized in the constructor of the class, then I'd have to hunt through your code to find the type. This can be overcome by leaving a comment telling me what type the auto variable is, however that seem to defeat the purpose.

    I hope you enjoyed my (now corrected) blog post. Cheers smile.png!

0
Sign in to follow this  
Followers 0


5 Comments


[quote name='Aardvajk' timestamp='1354338512']
Quote

Having them initialized when they're created seems to defeat the purpose. They just seem like a tool for lazy programmers rather than a useful type of variable for making decisions at runtime.

Not really. They are extremely useful in template functions where the type is not actually known by the programmer.

The point is that auto is in no way intended to be for making decisions at runtime. It is compile-time resolved.

Quote

Not being able to change their types just reinforces the point above. It'd be better, for me, to be able to change the type of the variable rather than have it be set in stone when you create it.

If you want variables with types that can be changed at runtime, solutions exist (boost::variant, Qt's QVariant etc). Such features are better implemented as libraries than built into the language as there are a wealth of different ways to achieve this and there is no need for a core language feature.

Your class implementation could be much improved. Perhaps look at how some of the ones I mentioned above implement varadic types in C++.
[/quote]

Changed, just now realized that if it has to be initialized in your code it's a compiler decision, not a runtime one. I'm going to stop making posts like these until I understand more now, and thanks for the corrections.
2

Share this comment


Link to comment
Agree with your corrections. auto is certainly ripe for abuse in the hands of bad programmers :)
1

Share this comment


Link to comment
I agree with the poorly named variablesdea. Naming an auto variable "holder" seems like a good example :-) .
0

Share this comment


Link to comment

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