Sign in to follow this  
jaikc

class pointers

Recommended Posts

is it possible to access a private class data member with a pointer pointing to its memory location or is this memory 'protected'? is it possible to write to a private data member in this way? i have a textures stored in my class that i need to access in my main rendering function however i dont want to return the entire texture since this would take a lot of extra memory. GLuint * CMenu::GetBGTexture(void) { return(&bgTexture); } glBindTexture(GL_TEXTURE_2D, *menu.GetBGTexture()); anyone know if this should work, or a better way of doing it?

Share this post


Link to post
Share on other sites
That will work, but I think you're misunderstanding something. The texture isn't stored in your class, your class is simply keeping a number (bgTexture) that refers to the texture (OpenGL itself does memory work).

So just return the bgTexture variable, not a pointer to it.

Eg:

GLuint CMenu::GetBGTexture(void)
{
return (bgTexture);
}

glBindTexture(GL_TEXTURE_2D, menu.GetBgTexture());


Share this post


Link to post
Share on other sites
Quote:
Original post by jaikc
is it possible to access a private class data member with a pointer pointing to its memory location or is this memory 'protected'? is it possible to write to a private data member in this way?

Yes. However, the only way to do this from outside the class would be to violate your class's encapsulation. That is, you'd need to provide a public or protected method which just returns a pointer to the private data, as shown above:

// C#
public class Foo
{
private Data m_Data;

public Data ViolatingMethod()
{
return this.m_Data; // return a reference to private data
}
}


Share this post


Link to post
Share on other sites
Quote:
Original post by kSquared
Yes. However, the only way to do this from outside the class would be to violate your class's encapsulation. That is, you'd need to provide a public or protected method which just returns a pointer to the private data, as shown above:


ok so would you have all the texture loading and rendering functions inside the class as well to avoid this? seems like a lot of duplicate code since i have at least 6 classes that will need textures loading (and probably many more in the future). any other way of doing it?

still need to get it working *at all* mind [wink]

Share this post


Link to post
Share on other sites
The wonders of code reusal... If your classes have a common functionality, group this functionality in another module (a "texture loading" class, for instance, or maybe a template).

Once your code is grouped inside such a module, simply use the module in objects that need the functionality (either by inheriting the objects from said class, or by giving them an instance of said class as a member).

Share this post


Link to post
Share on other sites
ok need to look into oop some more still kinda new to it, havent used inheritance before either

just had a long post typed up to try and find out why the texture wasnt loading properly, but just realized the texture loading function was being called from my xml parsing function which was being called before opengl got init'ed. *sigh* 3+hours debugging for that .. [depressed]

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