working with policy classes

A

aaragon

Hello everyone. I'm reading Andrei's Alexandrescu's book on Modern C++
Design and I decided to implemente in my research code the use of
policy classes. My objective is to abstract the storage of all
individuals in a population. To that purpose, I created a Population
class, as follows:

template <class T>
class CreationPolicy
{
public:
void create(size_t);
};

template <class T>
struct StdVectorStorage
{
public:
static std::vector<T>* _pointee;

static void create(size_t p)
{
_pointee = new std::vector<T>();
_pointee->reserve(p);
}
protected:
~StdVectorStorage(){}
};

template
<
class Individual,
template <class> class CreationPolicy = StdVectorStorage>
class Population : public CreationPolicy<Individual>
{
public:

Parameters* _Params;

// initialize population
void initialize(Parameters* p)
{
// assign pointer to parameter list
_gaParams = p;

// create population according to the CreationPolicy
this->create((size_t)_gaParams->get(popSize));
}
};

For some reason, if I try to use the _pointee value within initialize I
get a compiler error. From the user point of view, I don't want to
include another parameter for the instantiation of the Population class
(that's why I didn't include a Pointer parameter and a Pointer*
_variable within the class. Can anyone give me a hand on this? Thank
you very much,

Alejandro M. Aragón
 
A

amparikh

aaragon said:
Hello everyone. I'm reading Andrei's Alexandrescu's book on Modern C++
Design and I decided to implemente in my research code the use of
policy classes. My objective is to abstract the storage of all
individuals in a population. To that purpose, I created a Population
class, as follows:

template <class T>
class CreationPolicy
{
public:
void create(size_t);
};

template <class T>
struct StdVectorStorage
{
public:
static std::vector<T>* _pointee;

static void create(size_t p)
{
_pointee = new std::vector<T>();
_pointee->reserve(p);
}
protected:
~StdVectorStorage(){}
};

template
<
class Individual,
template <class> class CreationPolicy = StdVectorStorage>
class Population : public CreationPolicy<Individual>
{
public:

Parameters* _Params;

// initialize population
void initialize(Parameters* p)
{
// assign pointer to parameter list
_gaParams = p;

// create population according to the CreationPolicy
this->create((size_t)_gaParams->get(popSize));
}
};

For some reason, if I try to use the _pointee value within initialize I
get a compiler error. From the user point of view, I don't want to
include another parameter for the instantiation of the Population class
(that's why I didn't include a Pointer parameter and a Pointer*
_variable within the class. Can anyone give me a hand on this? Thank
you very much,

Alejandro M. Aragón

The code you have posted doesnt compile because the following arent
defined anywhere.
1> Parameters* _Params;
2>_gaparams.

Nonetheless, try accessing pointee (if that is indeed the problem..as I
dont see the code accessing it) as this->pointee.
 
A

aaragon

Hi and thanks for replying in such a short time! Well, the Parameters
are included in another file (I didn't put the inclusion of the header
file in the code). I did what you suggested (this->_pointee) and in
deed it worked!. Could you please explain me why? I'm using public
inheritance so I am supposed to access the _pointee variable without
having to use the this (right?).
I'm new using this kind of code and I've seen that for the policy
classes they use the static word in front of the functions but if I use
it I have a link error from the compiler. Any ideas on this?
 
A

amparikh

aaragon said:
Hi and thanks for replying in such a short time! Well, the Parameters
are included in another file (I didn't put the inclusion of the header
file in the code). I did what you suggested (this->_pointee) and in
deed it worked!. Could you please explain me why? I'm using public
inheritance so I am supposed to access the _pointee variable without
having to use the this (right?).
I'm new using this kind of code and I've seen that for the policy
classes they use the static word in front of the functions but if I use
it I have a link error from the compiler. Any ideas on this?

Its something got to do with "dependent names" and how they are looked
at.Look up dependent names in google ..even the faq at
www.parashift.com describes it.

if you declare a static member in a class, then you need to define it.
If you dont define it, that causes a linker error.
 

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

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,202
Latest member
MikoOslo

Latest Threads

Top