Jump to content

  • Log In with Google      Sign In   
  • Create Account

Pointer confusion


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.

  • You cannot reply to this topic
10 replies to this topic

#1 cozzie   Members   -  Reputation: 1616

Like
0Likes
Like

Posted 10 March 2014 - 03:48 PM

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, 10 March 2014 - 03:50 PM.


Sponsor:

#2 Rattenhirn   Crossbones+   -  Reputation: 1758

Like
7Likes
Like

Posted 10 March 2014 - 04:02 PM

In your case, all the pointers point to a const object, meaning you can't change the object through that pointer. You can, however, have the pointer point somewhere else, as is evident from the assignment of mD3dscene.

If you want the pointer itself const, you have to put a const after the *.

Examples:
int * ptr; // non const pointer to non const object
const int * ptr; // non const pointer to const object
int const * ptr; // non const pointer to const object (same as above)
int * const ptr = something; // const pointer to non const object
const int * const ptr = something; // const pointer to const object
int const * const ptr = something; // const pointer to const object (same as above)

I think these are all cases and I hope that helps!

#3 Buckeye   Crossbones+   -  Reputation: 5070

Like
1Likes
Like

Posted 10 March 2014 - 04:03 PM

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


Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.


#4 cozzie   Members   -  Reputation: 1616

Like
1Likes
Like

Posted 10 March 2014 - 04:25 PM

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, 10 March 2014 - 04:26 PM.


#5 Rattenhirn   Crossbones+   -  Reputation: 1758

Like
1Likes
Like

Posted 11 March 2014 - 03:54 AM

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



#6 cozzie   Members   -  Reputation: 1616

Like
0Likes
Like

Posted 11 March 2014 - 02:49 PM

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



#7 Aardvajk   Crossbones+   -  Reputation: 6010

Like
0Likes
Like

Posted 11 March 2014 - 03:18 PM

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.

#8 cozzie   Members   -  Reputation: 1616

Like
0Likes
Like

Posted 11 March 2014 - 03:35 PM

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



#9 King Mir   Members   -  Reputation: 2002

Like
0Likes
Like

Posted 11 March 2014 - 04:34 PM

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.

#10 Aardvajk   Crossbones+   -  Reputation: 6010

Like
0Likes
Like

Posted 12 March 2014 - 02:09 AM

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.



#11 cozzie   Members   -  Reputation: 1616

Like
0Likes
Like

Posted 12 March 2014 - 09:11 AM

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.




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.



PARTNERS