By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,965 Members | 2,055 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,965 IT Pros & Developers. It's quick & easy.

working with policy classes

P: n/a
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 <classclass 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

Sep 18 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a

aaragon wrote:
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 <classclass 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.

Sep 18 '06 #2

P: n/a
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?
ampar...@gmail.com wrote:
aaragon wrote:
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 <classclass 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.
Sep 18 '06 #3

P: n/a

aaragon wrote:
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.

>

ampar...@gmail.com wrote:
aaragon wrote:
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 <classclass 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 initializeI
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.
Sep 18 '06 #4

P: n/a
aaragon wrote:
Hi and thanks

Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.html>
Sep 18 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.