Dear all,
I try to compile the following code in two ways:
* using one compilation unit:
g++ main.cpp base.cpp -o main
* creating a library for the class factory.
g++ -c -o base.o base.cpp
ar rv libfactory.a base.o
g++ main.cpp -o main-lib -L. -lfactory
when using compilation unit, the base class is added in the base class
factory (the registerMe variable) and I can create an object at ruin-time.
When using the library the global object in the anonymous namespace
(registerMe) is not included, any idea why is that so?
Following are the necessary source codes.
Sincerely, Patrick
factory.h
--------------------------------------------------------------------------
template <class ManufacturedType>
class GenericFactory
{
public:
typedef ManufacturedType* BasePtr;
typedef std::string ClassIDKey;
private:
typedef BasePtr (*BaseCreateFn)(int, char **);
typedef std::map<ClassIDKey, BaseCreateFn> FnRegistry;
GenericFactory() {}
FnRegistry registry;
public:
static GenericFactory &instance()
{
static GenericFactory<ManufacturedType> bf;
return bf;
}
void RegCreateFn(const ClassIDKey & id, BaseCreateFn fn)
{
registry[id] = fn;
}
BasePtr create(const ClassIDKey &className,int nargs, char *args[])
const
{
BasePtr theObject(0);
typename FnRegistry::const_iterator
regEntry(registry.find(className));
if (regEntry != registry.end()) {
theObject = regEntry->second(nargs,args);
} else {
throw std::string("Error: unrecognised ClassIDKey: "+className);
}
return theObject;
}
};
template <class AncestorType, class ManufacturedType>
struct RegisterInFactory
{
typedef typename GenericFactory<AncestorType>::BasePtr BasePtr;
typedef typename GenericFactory<AncestorType>::ClassIDKey ClassIDKey;
static BasePtr CreateInstance(int nargs, char *args[])
{
return BasePtr(new ManufacturedType(nargs, args));
}
RegisterInFactory(const ClassIDKey &id)
{
GenericFactory<AncestorType>::instance().RegCreate Fn(id,
CreateInstance);
}
};
--------------------------------------------------------------------------
base.h
--------------------------------------------------------------------------
struct Base
{
Base(int nargs=0, char *args[]=0);
virtual ~Base();
virtual void whoami();
};
--------------------------------------------------------------------------
base.cpp
--------------------------------------------------------------------------
#include "factory.h"
#include "base.h"
Base::Base(int nargs, char *args[])
{
std::cout << "Base class created with " << nargs << " arguments: ";
if (nargs > 0) {
std::cout << args[0];
for (int i=1; i<nargs; i++) std::cout << ", " << args[i];
}
std::cout << std::endl;
}
Base::~Base() {}
void Base::whoami()
{
std::cout << "I am a Base class" << std::endl;
}
namespace {
RegisterInFactory<Base, Base> registerMe("Base");
}
--------------------------------------------------------------------------
main.cpp
--------------------------------------------------------------------------
#include "factory.h"
#include "base.h"
typedef GenericFactory<Base> Factory;
typedef Factory::BasePtr BasePtr;
int main(int nargs, char *args[])
{
try {
BasePtr object(Factory::instance().create("Base", nargs, args));
object->whoami();
return 0;
} catch (std::string &msg) {
std::cerr << msg << std::endl;
return 0;
}
}
-------------------------------------------------------------------------- 6 1315
* Patrick Guio: When using the library the global object in the anonymous namespace (registerMe) is not included, any idea why is that so?
Static initialization happens before the first call to a function in the
translation unit, see §3.6.2/3. You have no such call. Add such a call
in 'main'.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
* Alf P. Steinbach: * Patrick Guio: When using the library the global object in the anonymous namespace (registerMe) is not included, any idea why is that so?
Static initialization happens before the first call to a function in the translation unit, see §3.6.2/3. You have no such call. Add such a call in 'main'.
Explanation only partially correct (there is an exception, namely for an
object where the constructor has a side effect, which is your case), but
the cure I stated will, hopefully, be effective nonetheless...
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
On Thu, 20 Jan 2005, Alf P. Steinbach wrote: When using the library the global object in the anonymous namespace (registerMe) is not included, any idea why is that so?
Static initialization happens before the first call to a function in the translation unit, see §3.6.2/3. You have no such call. Add such a call in 'main'.
Hi Alf,
I am not sure what are you referencing to with §3.6.2/3? What should thiis
function contain?
Sincerely, Patrick
On Thu, 20 Jan 2005, Alf P. Steinbach wrote: When using the library the global object in the anonymous namespace (registerMe) is not included, any idea why is that so?
Static initialization happens before the first call to a function in the translation unit, see §3.6.2/3. You have no such call. Add such a call in 'main'.
Explanation only partially correct (there is an exception, namely for an object where the constructor has a side effect, which is your case), but the cure I stated will, hopefully, be effective nonetheless...
Hi again,
Which class constructor are you refering to? Base, RegisterInFactory
or GenericFactory? And what kind of side effect do you mean?
Sincerely, Patrick
* Patrick Guio: This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools.
Please don't do that.
Which class constructor are you refering to? Base, RegisterInFactory or GenericFactory? And what kind of side effect do you mean?
RegisterInFactory, and the kind of side effect when the standard talks
about that is a change of process state, i.e. changing the values of
variables outside the object.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
* Patrick Guio: This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools.
Please don't do that.
I am not sure what are you referencing to with §3.6.2/3?
The C++ standard.
What should thiis function contain?
It can be an empty, dummy function; the main thing is that it's called.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: A |
last post by:
Hi,
Consider this code:
//Header File - Foo.h
int i = 0; // non-static global variable
class Foo{
...
|
by: James N |
last post by:
Here's the situation: I have 3 webforms in my project. Form 1 allows
the user to select a type of report to generate. There are 2 possible
type of reports, and therefore, Forms 2 and 3 are these...
|
by: Faisal |
last post by:
Hi.
I'm in the process of moving an application from ASP to ASP.NET, & I'm
writing in VB, using VS.NET. I'm new to the .NET framework & have a
basic question regarding static objects defined in...
|
by: Rick Hein |
last post by:
I've got a problem with an app I've been working on, the Caching object and
events not firing correctly. In a nutshell: When I'm debugging, and I set a
breakpoint in the removed item call back, the...
|
by: CDMAPoster |
last post by:
About a year ago there was a thread about the use of global variables
in A97:
http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157
Best Practices by Kang...
|
by: weaknessforcats |
last post by:
C++: The Case Against Global Variables
Summary
This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
|
by: taps128 |
last post by:
I've been reading the namespace specification for the 5.3 relaese, and I
can't stop thinking that they have complicated the thing unecessary.
Here is what I mean.
So far if you call a function...
|
by: sap0321 |
last post by:
This should be kindergarten stuff but for some reason I am having trouble with it. I do 99.9% web programming and this is the first windows app i've done in years - probably the first ever in C# ......
|
by: Rajesh |
last post by:
I read Global Object's constructor will be called before main()
function;
In which situation it can be really helpful? Is it good practice use
Global object and its constructor ?
Thanks,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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,...
| |