• 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
noatom

const problems

5 posts in this topic

class x{
int i;
public:
x(int ii=0);
void modify();
};

X f5(){
return X();
}

void f7(X& x){
x.modify();
}

int main(){
f7(f5()); <-----COMPILE ERROR THERE

}

 

Can someone please explain why does that error appear ?

0

Share this post


Link to post
Share on other sites

It doesn't say const anywhere in the code!

 

Presumably it is moaning that you are passing an unnamed temporary value (i.e. return value from f5()) as a reference parameter which isn't legal (but may be allowed with a warning) since you don't want to be modifying an unnamed temporary value by accident.

 

Try doing

 

X myX = f5();

 

f7(myX);

 

instead, that should work...

 

EDIT: And we aren't psychic (try the math & psychics forum instead for that) please post the full error message and compiler you are using next time kthx ;)

Edited by Paradigm Shifter
1

Share this post


Link to post
Share on other sites
The return from a function is an rvalue, so cannot be bound to a non-const lvalue reference like X & (without a vendor extension like one that MSVC has) but can be bound to a const lvalue reference. With a C++11 compiler you can also bind the return value to a function to an rvalue reference like X &&.
2

Share this post


Link to post
Share on other sites
In simpler terms...

Every expression in c++ is either an lvalue or an rvalue. The names come from which side of the assignment operator they can exist on.

An lvalue is (basically) any named object. All variables are lvalues. An lvalue is something that can exist for more than a single statement.

An rvalue is a temporary value that does not necessarily exist for more than a single statement. The result of a function, as in you example, is an rvalue. The value exists only for a single expression then is lost.

Compilers are very aggressive at optimizing rvalues out of existence; often they only exist in a register or can be sometimes eliminated completely.

You cannot take an address of an rvalue. It is temporary and may not even really exist.

You need to create an lvalue, and that lvalue can be passed by reference to the second function.

Alternatively you can pass it as a const reference. The compiler is allowed to (essentially) create a temporary constant object and pass that object by reference.


Or written differently: because it is passed by non-const reference you must create a variable, assign the first function's result to the variable, and then pass the variable to the second function. Or you will need to modify your function to accept a const reference; do this if you can guarantee you will never modify the input.
2

Share this post


Link to post
Share on other sites

I think I won the "in simpler terms" game!

 

I was going to suggest using a const reference for function f7 but it calls a function called modify() on the parameter passed, I guessed that might not be a good suggestion, so I made a poor joke instead ;)

0

Share this post


Link to post
Share on other sites

class x{
int i;
public:
x(int ii=0);
void modify();
};

X f5(){
return X();
}

void f7(X& x){
x.modify();
}

int main(){
f7(f5()); <-----COMPILE ERROR THERE

}

 

Can someone please explain why does that error appear ?

 

Beyond the very descriptive and detailed descriptions, this could be a simple case of compiler differences biting you on the ass.  The given code is valid for Visual Studio and unless you set warning level to 4, it won't even bitch about this.  The very simple answer is that:

 

f7( f5() );

 

is actually illegal C++ yet VC cheats and allows it via a syntactic sugar addition.  GCC on the other hand will pitch a fit and give you an error.  The "fix" is simple:

 

temp = f5();

f7( temp );

 

So, if you have been using GCC and reading books with a VC source (pretty common unfortunately), you need to use the more explicit syntax, VC does this behind your back and hides it unless you set warnings to 4.  (Not even sure VC warns about this at w4, think so though.)

1

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