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

Pointer confusion

10 posts in this topic

Hi,

I have a class "A" in which I want to have a reference/ pointer to an object of class "B" for usage within the member functions of A.

Currently I do this:

// class A definition

	const Crealysm_d3drenderer::CD3dscene* mD3dscene;

	// Initial - creation of the renderqueue
	bool Create(const Crealysm_d3drenderer::CD3dscene *pD3dscene, const int pLightingEffectId);

// class A implementation

bool CRenderQueue::Create(const Crealysm_d3drenderer::CD3dscene *pD3dscene, const int pLightingEffectId)
{
	if(!pD3dscene->IsLoaded()) return false;

	if((mD3dscene = pD3dscene) == NULL) return false;	// set const pointer to d3dscene

Functionally this all works, but I believe I've only made the pointer const, so the actual pointer is const instead of the object it points to.

Although I'm not sure, because I'm not able to make changes to the objects it points to (pD3dscene). 

 

Can someone clear this up for me?

Edited by cozzie
0

Share this post


Link to post
Share on other sites

First, you can't have an uninitialized reference as a member. If D3dscene is a global object, you can declare "extern CD3dscene D3dscene;" and declare as a member "CD3dscene& myD3dscene = D3dscene;" Not a very good solution, though.

 

Perhaps a better approach is to let pD3dscene protect itself, by declaring members as protected or private as appropriate, and providing access to those members via const return values.

 

(By the way, it's more common that what you're calling a definition is a declaration, and your implementation is the definition.)

1

Share this post


Link to post
Share on other sites

Thanks, I've changed it into a const pointer to a const object, in the create member function.

It works. I can now not longer call non-const member functions with mD3dscene (member).

 

The member pointer of the renderqueue can ofcourse still be modified (point to something else), but that's logical I think.

bool CRenderQueue::Create(const Crealysm_d3drenderer::CD3dscene *const pD3dscene, const int pLightingEffectId)
{
	if(!pD3dscene->IsLoaded()) return false;

	if((mD3dscene = pD3dscene) == NULL) return false;	// set const pointer to d3dscene

?

	const Crealysm_d3drenderer::CD3dscene* mD3dscene;

@Buckeye; I've actually protected all members of CD3dscene correctly, but there are still ways through public functions to make changes in the d3dscene, which I do not want the renderqeue to be able to. Thanks for the correct naming, will remember it.

Edited by cozzie
1

Share this post


Link to post
Share on other sites

One more note, making by value parameters (like the pointer pD3dscene and pLightingEffectId in your example) const does not really give you any benefits, because the values are copied anyways.

 

But it's a matter of personal preference really...

1

Share this post


Link to post
Share on other sites

Thanks, when it's not necessary I fully agree.

I've been playing around just to make sure I understand correct;

 

- the pD3dscene only has to be "const CD3dscene *pScene", that way my goal is achieved that within renderqueue, after copying the pointer, the mD3dscene cannot be altered

- adding the extra const only makes sure that within the Create function pd3dscene (the actual pointer) cannot be altered, which is unlikely to happen

 

Regarding the pLightingEffectId it's sort of the same, but I most of the time don't take the risk that member functions of classed are allowed to change passed values which are needed within the memberfunctions (just to be sure, I could make a mistake by accidentally changing passed values).

0

Share this post


Link to post
Share on other sites
Irrelevant for context but just for understanding pointers, remember that pD3dscene is a copy of the pointer that you pass in at call site and modifying this copy will not affect the pointer in the calling scope that you pass to the function.

Hence why the second const is a bit redundant. It's free and states intent but you won't see much of this in other peoples code.
0

Share this post


Link to post
Share on other sites

Thanks, I understand. But still within the 'Create' function I could change pD3dscene as a pointer itself, make it NULL or point to something else by accident (although that's not very likely and in this case can only happen before copying it to m3dscene).

0

Share this post


Link to post
Share on other sites
If you're looking to change a pointer, pass in a pointer to pointer. If you want to be able change the passed in pointer this way, but not the object pointed to, the syntax is:

const Crealysm_d3drenderer::CD3dscene * *var
You can also pass a pointer by reference.

const Crealysm_d3drenderer::CD3dscene * &var
They difference between the two is a matter of style.
0

Share this post


Link to post
Share on other sites

Thanks, I understand. But still within the 'Create' function I could change pD3dscene as a pointer itself, make it NULL or point to something else by accident (although that's not very likely and in this case can only happen before copying it to m3dscene).

 

Perfectly true and you seem to understand the scope of the pointer okay. In theory we should all be this specific about const in function parameters since it makes the code entirely self-documented, but in practice the my feeling is the additional complexity to the reader outweighs the benefits but its entirely up to you.

 

But this is like:
 

void setLives(const int number);

Which you don't tend to see, even if number is not supposed to be modified by the function. Its technically correct, but it doesn't offer the same benefits as a const reference (can be initialised with a temporary) or const member functions (can be selected based on the object's own constness) so people tend not to bother. Maybe we should.

 

The point is the areas people do religiously use const offer slightly wider benefits than just preventing you accidentally changing a variable in a local scope.

0

Share this post


Link to post
Share on other sites
Thanks, I fully agree. In my case I always try to be cost correct/ on the clear and safe side. 80/20 that is, it should become a goal on its own. For example: bool CRenderable::SetVisible(const bool pVisible) is an example of what my standard approach is.
0

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