global/static/namespace trouble

P

Patrick Guio

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().RegCreateFn(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;
}
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;
}
}
--------------------------------------------------------------------------
 
A

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'.
 
A

Alf P. Steinbach

* Alf P. Steinbach:
* Patrick Guio:

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...
 
P

Patrick Guio

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
 
P

Patrick Guio

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
 
A

Alf P. Steinbach

* 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

Alf P. Steinbach

* 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.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top