473,883 Members | 1,602 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Singleton and static function

Hi,

I have a singleton class defined like this :

class UIManager : public CSingleton<UIMa nager>,
public CObject
{
protected:
DECLARE_DYNAMIC (UIManager)
friend class CSingleton<UIMa nager>;

UIManager();
virtual ~UIManager();

public:
....
};

and I was using this code like this :

A)
UIManager* l_pUiMgr = UIManager::GetI nstance();
ASSERT (l_pUiMgr != NULL);
l_pUiMgr->GetResText( a_ResId, bStripHtml);
But I was fed up with always typing this so I have declared below my
UIManager class a static function :

static inline UIManager& UIManager() { return *(UIManager::Ge tInstance()); }
and I wanted to be able to call it like that :

UIManager().Get ResText( a_ResId, bStripHtml);

The problem is I get some compilations errors with the code in A)

5>c:\wce_v42\in c\BaseView.h(22 7) : error C2065: 'l_pUiMgr' : undeclared
identifier

Why I cannot write UIManager* now ?

Oct 31 '08 #1
4 2834
On Oct 31, 3:27*pm, John Doe <mos...@anonymo us.orgwrote:
Hi,

I have a singleton class defined like this :

class UIManager : public CSingleton<UIMa nager>,
* * * * * * * * * * * * * * * * * public CObject
{
protected:
* * * * DECLARE_DYNAMIC (UIManager)
* * * * friend class CSingleton<UIMa nager>;

* * * * UIManager();
* * * * virtual ~UIManager();

public:
...

};
What do you need that CSingleton<for? Can you not just have a global
object?
and I was using this code like this :

A)
UIManager* l_pUiMgr = UIManager::GetI nstance();
ASSERT (l_pUiMgr != NULL);
l_pUiMgr->GetResText( a_ResId, bStripHtml);
That ASSERT is probably useless because if UIManager is allocated on
the heap (using new) it throws an exception. It it it allocated as a
function static variable, than it can never be NULL.
But I was fed up with always typing this so I have declared below my
UIManager class a static function :

static inline UIManager& UIManager() { return *(UIManager::Ge tInstance()); }

and I wanted to be able to call it like that :

UIManager().Get ResText( a_ResId, bStripHtml);
Which looks better.
The problem is I get some compilations errors with the code in A)

5>c:\wce_v42\in c\BaseView.h(22 7) : error C2065: 'l_pUiMgr' : undeclared
identifier

Why I cannot write UIManager* now ?
This error says that you are accessing l_pUiMgr object, which is not
available from the current scope. Either you misspelled it or did not
include the necessary header files.

--
Max
Oct 31 '08 #2
On Oct 31, 10:27*am, John Doe <mos...@anonymo us.orgwrote:
Hi,

I have a singleton class defined like this :

class UIManager : public CSingleton<UIMa nager>,
* * * * * * * * * * * * * * * * * public CObject
{
protected:
* * * * DECLARE_DYNAMIC (UIManager)
* * * * friend class CSingleton<UIMa nager>;

* * * * UIManager();
* * * * virtual ~UIManager();

public:
...

};

and I was using this code like this :

A)
UIManager* l_pUiMgr = UIManager::GetI nstance();
ASSERT (l_pUiMgr != NULL);
l_pUiMgr->GetResText( a_ResId, bStripHtml);

But I was fed up with always typing this so I have declared below my
UIManager class a static function :

static inline UIManager& UIManager() { return *(UIManager::Ge tInstance()); }

and I wanted to be able to call it like that :

UIManager().Get ResText( a_ResId, bStripHtml);

The problem is I get some compilations errors with the code in A)

5>c:\wce_v42\in c\BaseView.h(22 7) : error C2065: 'l_pUiMgr' : undeclared
identifier

Why I cannot write UIManager* now ?
Why don't you tell us? l_pUiMgr is an undeclared identifier. You are
doing something wrong, what that might be we don't know. Put together
a simple, compileable case forth without MS-specific macros.
Oct 31 '08 #3
Salt_Peter a écrit :
On Oct 31, 10:27 am, John Doe <mos...@anonymo us.orgwrote:
>Hi,

I have a singleton class defined like this :

class UIManager : public CSingleton<UIMa nager>,
public CObject
{
protected:
DECLARE_DYNAMIC (UIManager)
friend class CSingleton<UIMa nager>;

UIManager();
virtual ~UIManager();

public:
...

};

and I was using this code like this :

A)
UIManager* l_pUiMgr = UIManager::GetI nstance();
ASSERT (l_pUiMgr != NULL);
l_pUiMgr->GetResText( a_ResId, bStripHtml);

But I was fed up with always typing this so I have declared below my
UIManager class a static function :

static inline UIManager& UIManager() { return *(UIManager::Ge tInstance()); }

and I wanted to be able to call it like that :

UIManager().Ge tResText( a_ResId, bStripHtml);

The problem is I get some compilations errors with the code in A)

5>c:\wce_v42\i nc\BaseView.h(2 27) : error C2065: 'l_pUiMgr' : undeclared
identifier

Why I cannot write UIManager* now ?

Why don't you tell us? l_pUiMgr is an undeclared identifier. You are
doing something wrong, what that might be we don't know. Put together
a simple, compileable case forth without MS-specific macros.

I mean that the following line doesn't compile anymore :
UIManager* l_pUiMgr = UIManager::GetI nstance();
but maybe I forgot one include ...
Oct 31 '08 #4
On Oct 31, 3:50*pm, Mosfet <mos...@anonymo us.orgwrote:
Salt_Peter a écrit :


On Oct 31, 10:27 am, John Doe <mos...@anonymo us.orgwrote:
Hi,
I have a singleton class defined like this :
class UIManager : public CSingleton<UIMa nager>,
* * * * * * * * * * * * * * * * * public CObject
{
protected:
* * * * DECLARE_DYNAMIC (UIManager)
* * * * friend class CSingleton<UIMa nager>;
* * * * UIManager();
* * * * virtual ~UIManager();
public:
...
};
and I was using this code like this :
A)
UIManager* l_pUiMgr = UIManager::GetI nstance();
ASSERT (l_pUiMgr != NULL);
l_pUiMgr->GetResText( a_ResId, bStripHtml);
But I was fed up with always typing this so I have declared below my
UIManager class a static function :
static inline UIManager& UIManager() { return *(UIManager::Ge tInstance()); }
and I wanted to be able to call it like that :
UIManager().Get ResText( a_ResId, bStripHtml);
The problem is I get some compilations errors with the code in A)
5>c:\wce_v42\in c\BaseView.h(22 7) : error C2065: 'l_pUiMgr' : undeclared
identifier
Why I cannot write UIManager* now ?
Why don't you tell us? l_pUiMgr is an undeclared identifier. You are
doing something wrong, what that might be we don't know. Put together
a simple, compileable case forth without MS-specific macros.

I mean that the following line doesn't compile anymore :
UIManager* l_pUiMgr = UIManager::GetI nstance();
but maybe I forgot one include ...- Hide quoted text -

- Show quoted text -
I had no problem, then again, i don't need MS code to do any of it.
i'ld suggest declaring a pointer only (which does not construct
anything) just to find out if UIManager is recognized as a complete
type.

UIManager* p_mgr;

If that fails then includes are indeed the issue and you should get an
error specifically stating what is not recognized as a known type. I
have no clue what header CSingleton is found in (only a Singleton can
access UIManager's protected constructor).

I will say that your setup should look like the following, which does
work, although my Singleton type needs a little more tightening to
prevent side-effects.

[OT]
MS and MFC as well as other languages like Java live in an 'everything
is an Object or CObject' world. As if an instance was somehow
otherwise NOT an object. That mentality is bad, nasty, buggy, insane,
dumb, stupid, etc.
[/OT]

#include <iostream>

class Object { };

template< typename T >
class Singleton
{
protected:
Singleton() { std::cout << "Singleton()\n" ; }
~Singleton() { std::cout << "~Singleton()\n "; }
private:
Singleton(const Singleton& copy);
Singleton& operator= (const Singleton&);
public:
static T* const GetInstance()
{
static T t;
std::cout << "&t = " << &t << std::endl;
return &t;
}
};

class Manager : public Singleton< Manager >, public Object
{
friend class Singleton<Manag er>;
protected:
Manager() { std::cout << "Manager()\ n"; }
virtual ~Manager() { std::cout << "~Manager() \n"; }
};

int main()
{
Manager* p_mgr = Manager::GetIns tance();
std::cout << "Manager* p_mgr = ";
std::cout << p_mgr << std::endl;
}

Nov 1 '08 #5

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

Similar topics

1
2447
by: Phil Powell | last post by:
Consider this: class ActionHandler { /*------------------------------------------------------------------------------------------------------------------------------------------------------------------- This class will be a singleton class structure by calling its constructor by reference only (prefixed by '&'). Is primarly used to reference its errorArray Array property as a single reference to allow for static usage
11
2359
by: [Mystic] | last post by:
I was just wondering something about this. I wanted to do something similar, like having a mysql class that there can only be one of. The purpose of this was not to have hundereds to mysql connections open, and also to track how many queries are being sent/second. When creating the singleton, I assume you use this syntax: $var = &new singelton;
3
2505
by: Alicia Roberts | last post by:
Hello everyone, I have been researching the Singleton Pattern. Since the singleton pattern uses a private constructor which in turn reduces extendability, if you make the Singleton Polymorphic what sort of problems/issues should be considered? Also, I see that a singleton needs to be set up with certain data such as file name, database URL etc. What issues are involved in this, and how would you do this? If someone knows about the...
3
3933
by: Harry | last post by:
Hi ppl I have a doubt on singleton class. I am writing a program below class singleton { private: singleton(){}; public: //way 1
5
4718
by: LinuxGuy | last post by:
Hi, I have come across singleton class with some member variables are declared as static with public scope. As singleton class always return only one instance. ie. single copy of object is maintained all the time. can someone tell me the reason behind declaring those variables as static one.
10
1487
by: A_StClaire_ | last post by:
hi, I have a singleton Evaluation class that I'm calling repeatedly in one sequence. would someone plz have a look at the code below and tell me if one instance of the singleton can ever "clash" with another? I'm asking because I'm getting unexpected results. I've looked for the problem the past several days and can't attribute the anomalies to logic errors.
6
2287
by: Manuel | last post by:
Consider the classic singleton (from Thinking in C++): ----------------------------------------------------- //: C10:SingletonPattern.cpp #include <iostream> using namespace std; class Singleton { static Singleton s; int i; Singleton(int x) : i(x) { }
5
21173
by: Damien | last post by:
Hi all, I'm using a pretty standard C++ Singleton class, as below: template <typename T> class Singleton { public: static T* Instance() {
10
3336
by: wkaras | last post by:
Why aren't "Singletons" done like this in C++: A.hpp: class A { public: static A theA; // ...
0
9933
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11114
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10835
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9563
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7960
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7114
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5787
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4605
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4205
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.