Problem: global variables and singleton

M

Manuel

I'm trying to continue development of my little opensource GUI.
Now I'm studying the texture in openGL. I know here is OT talk of
specific openGL, but the question is not about the usage of this lib,
but about a (perhaps) general case, very technical.

To resume briefly, I've two files:
main.cpp
mhwindow.cpp

the second contain a singleton I use to store global variables.
The singleton is very simple (I've reduced it to post this question):

----------------------------------------------------
class MHwindow {
private:
static MHwindow MHw;

GLuint texture;

//Constructor is private, because this is a singleton
MHwindow(){};
MHwindow& operator=(MHwindow&); // Disallowed
MHwindow(const MHwindow&); // Disallowed

public:
static MHwindow& instance() { return MHw; }

void setTexture(GLuint);
GLuint getTextures();
};


#endif //MHWINDOW_H
-----------------------------------------------------

the setTexture(GLuint) method set a private GLuint texture variable.

OK.
In main code I use a function that return a GLuint.
This psudocode work fine:

-----------main.cpp------------

GLuint myVar;
myVar = function_that_return_a_GLuint()

-------------------------------


This pseudocode don't work:

-----------main.cpp------------

singleton mysingleton;
mysingleton.setTexture(function_that_return_a_GLuint())

-------------------------------

I don't understand the reason. Independently from
function_that_return_a_GLuint(), both the cases should be identical:

in first case I assign the value to a global GLuint variable,
in second case I assing the value to a singleton GLuint variable,

but it's always a GLuint variable, so I don't understand the reason
because it not work.

If you want try, the files (I've minimized them) are here:

www.dedalo-3d.com/lab/try_ogl_texture.zip

The files as are work fine.

but, if you comment "textureID = pngBind etc..." and decomment the line:

MainWindow.setTexture(pngBind("pippo.png", PNG_NOMIPMAP, PNG_ALPHA,
&texInfo, GL_CLAMP, GL_NEAREST, GL_LINEAR));

it don't work.
Ah...to try the code, you need little glpng lib:
http://www.wyatt100.freeserve.co.uk/download.htm

Thanks,

Manuel
 
S

Shark

Why are you making the instance private? If i am correct you cannot
return a reference to a private object.

class MHwindow {
private:
static MHwindow MHw

==============

A better way to do it as follows:

class A {
static A& Get()
{
static A;
return A;
}
private:
A();
A(const A&);
A& operator=(const A&);

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top