473,396 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Problem: global variables and singleton

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_GLui nt())

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

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
Jan 13 '06 #1
2 2143
Just deleted some extra files from zip.
They was included because my error.
Sorry!
Jan 13 '06 #2
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&);

}

Jan 13 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tony Johansson | last post by:
Hello! I'm reading a book about C++ and there is something that I don't understand so I ask you. Below I have the text from the book and the code from the file where main is located and some...
2
by: David McCormack | last post by:
I have a small project that I'm writing to help me learn C# and the .Net Framework. This project is a WinForms program with multiple forms that accesses MSDE. I've got most of it done but I've hit...
19
by: Shiv Kumar | last post by:
I see that the Application_OnStart event is fired only once for a certain application. I'm interested in creating a "global" object (an object that will be available to all requests for the...
22
by: UJ | last post by:
How do I create an instance of a class that is accessible to all classes/forms in my project? I already have a startup module and I was thinking putting stuff in there and then accessing it through...
15
by: Sandy | last post by:
My background is web based. I am attempting to write a Windows application and I am finding some simple things difficult. Currently I am trying to find out how to store information session wide....
11
by: Manuel | last post by:
This is the singleton I'm trying to write: global.h ---------------------------------------- class Global { int i; static Global glob; //Global(int x): i(x) { } ; Global();
5
by: tobias.sturn | last post by:
Hi! I have written this template for making a singleton: #define DECLARE_SINGLETON(classname) \ private: \ static classname* m_pThis; \ classname(); \ class Guard \ { \ public: \
23
by: David Colliver | last post by:
Hi, using c#, 1.1 I know that we are not supposed to use global variables etc. in c# I am having a problem, but not sure how to resolve. I did have another post here, but may have over...
9
by: Tony Proctor | last post by:
I need to create a process Singleton object for an ASP application, but I'm having some odd issues In my GLOBAL.ASA, I have an <OBJECTelement specifying the relevant ProgID with RUNAT=Server and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.