473,395 Members | 1,442 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 <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
4 1739

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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Martin Vorbrodt | last post by:
I'm designing a Matrix class of 4x4 matrices used in computer graphics. Both OpenGL and Direct3D can take a pointer to array of 16 floats which represent the values in the matrix. OGL takes it in...
29
by: Patrick | last post by:
I have the following code, which regardless which works fine and logs to the EventViewer regardless of whether <processModel/> section of machine.config is set to username="SYSTEM" or "machine" ...
13
by: | last post by:
Hi all, I'm having some problems after upgrading a windows 2000 Server to Domain Controller. Symptoms: every aspx page returns blank HTTP Response headers: HTTP/1.0 200 OK Server:...
5
by: Martin Heuckeroth | last post by:
Hi We are working on a webservice application and are having some problems with the cookies and/or sessions. We have them working on our intranet but then its not working on the internet. We...
0
by: fcetrini | last post by:
Hello everybody, I'm using visual studio .net 1.1 for my applications. Source codes are located on a lan server. Until last week I was able to compile and run mi applications according to the...
6
by: eulaersivan | last post by:
I would like to use the xmlhttprequest-object to send an http request to my server. The http request is used to switch the light on through home automation. However it's not working, and I can't...
4
by: =?Utf-8?B?SmFu?= | last post by:
I have a .NET 2.0 application divided in two assemblies; the exe and a dll. The application generates a plugin-dll which is then loaded in a separate AppDomain (along with a second instance of my...
0
by: er | last post by:
hi, i'm trying to understand A) policy classes and see if they meet my needs. the book "C++ templates" has something similar called B) "bridge pattern implemented with templates". would it be...
3
by: DerrickH | last post by:
I have a template class with three policies: PolicyA, PolicyB, and PolicyC. The design dilemma I am having is that B and C depend upon A, but I would like my Host class to derive from all three. In...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
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...
0
Oralloy
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.