473,378 Members | 1,317 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,378 software developers and data experts.

Singleton and static function

Hi,

I have a singleton class defined like this :

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

UIManager();
virtual ~UIManager();

public:
....
};

and I was using this code like this :

A)
UIManager* l_pUiMgr = UIManager::GetInstance();
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::GetInstance()); }
and I wanted to be able to call it like that :

UIManager().GetResText( a_ResId, bStripHtml);

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

5>c:\wce_v42\inc\BaseView.h(227) : error C2065: 'l_pUiMgr' : undeclared
identifier

Why I cannot write UIManager* now ?

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

I have a singleton class defined like this :

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

* * * * 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::GetInstance();
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::GetInstance()); }

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

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

5>c:\wce_v42\inc\BaseView.h(227) : 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...@anonymous.orgwrote:
Hi,

I have a singleton class defined like this :

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

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

public:
...

};

and I was using this code like this :

A)
UIManager* l_pUiMgr = UIManager::GetInstance();
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::GetInstance()); }

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

UIManager().GetResText( a_ResId, bStripHtml);

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

5>c:\wce_v42\inc\BaseView.h(227) : 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...@anonymous.orgwrote:
>Hi,

I have a singleton class defined like this :

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

UIManager();
virtual ~UIManager();

public:
...

};

and I was using this code like this :

A)
UIManager* l_pUiMgr = UIManager::GetInstance();
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::GetInstance()); }

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

UIManager().GetResText( a_ResId, bStripHtml);

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

5>c:\wce_v42\inc\BaseView.h(227) : 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::GetInstance();
but maybe I forgot one include ...
Oct 31 '08 #4
On Oct 31, 3:50*pm, Mosfet <mos...@anonymous.orgwrote:
Salt_Peter a écrit :


On Oct 31, 10:27 am, John Doe <mos...@anonymous.orgwrote:
Hi,
I have a singleton class defined like this :
class UIManager : public CSingleton<UIManager>,
* * * * * * * * * * * * * * * * * public CObject
{
protected:
* * * * DECLARE_DYNAMIC(UIManager)
* * * * friend class CSingleton<UIManager>;
* * * * UIManager();
* * * * virtual ~UIManager();
public:
...
};
and I was using this code like this :
A)
UIManager* l_pUiMgr = UIManager::GetInstance();
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::GetInstance()); }
and I wanted to be able to call it like that :
UIManager().GetResText( a_ResId, bStripHtml);
The problem is I get some compilations errors with the code in A)
5>c:\wce_v42\inc\BaseView.h(227) : 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::GetInstance();
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<Manager>;
protected:
Manager() { std::cout << "Manager()\n"; }
virtual ~Manager() { std::cout << "~Manager()\n"; }
};

int main()
{
Manager* p_mgr = Manager::GetInstance();
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
by: Phil Powell | last post by:
Consider this: class ActionHandler { ...
11
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...
3
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...
3
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
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 ...
10
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...
6
by: Manuel | last post by:
Consider the classic singleton (from Thinking in C++): ----------------------------------------------------- //: C10:SingletonPattern.cpp #include <iostream> using namespace std; class...
5
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
by: wkaras | last post by:
Why aren't "Singletons" done like this in C++: A.hpp: class A { public: static A theA; // ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.