 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
aaragon Guest
|
Posted: Mon Sep 18, 2006 9:10 am Post subject: working with policy classes |
|
|
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 |
|
| Back to top |
|
 |
aaragon Guest
|
Posted: Mon Sep 18, 2006 9:10 am Post subject: Re: working with policy classes |
|
|
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:
| Quote: | 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 <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. |
|
|
| Back to top |
|
 |
Guest
|
Posted: Mon Sep 18, 2006 9:10 am Post subject: Re: working with policy classes |
|
|
aaragon wrote:
| Quote: | 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.
| Quote: |
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 <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. |
|
|
| Back to top |
|
 |
Guest
|
Posted: Mon Sep 18, 2006 9:10 am Post subject: Re: working with policy classes |
|
|
aaragon wrote:
| Quote: | 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. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|