class pointers

Started by
4 comments, last by jaikc 19 years, 4 months ago
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?
Advertisement
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());


-----BEGIN GEEK CODE BLOCK-----Version: 3.12GCS/M/S d->+(++) s+: a19? C++++ UL++ P+++ L+ !E W+++ N+ o++ K? w!O M-- V? !PS PE Y+ PGP t++ 5+++ X R tv+> b+(++)>+++ DI+++>+++++ D++G e>++++ h! r y?------END GEEK CODE BLOCK------
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  }}
- k2"Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ} | {Blog/Journal} | {[email=kkaitan at gmail dot com]e-mail me[/email]} | {excellent webhosting}
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]
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).
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]

This topic is closed to new replies.

Advertisement