使用策略类自定义结构 [英] Customizing structure with policy classes
问题描述
大家好。我正在尝试写一个基于策略的设计课程
(Alexandrescu的现代C ++设计)。我不是程序员,而是一名工程师,所以这对我来说很难。通过使用策略,
我想自定义一个类的结构。我们的想法是:b $ b调查几种数据结构的使用。一个选项是使用boost动态bitset
。另一个是使用
std :: vector。我获得了一些正常工作的代码,如下所示。
有些东西我不喜欢代码,这就是我要求的原因。发布此消息。
1.在当前版本中,用户必须键入
ClassA< double,VectorPolicyca(6);
来创建ClassA的对象。我想将此更改为
ClassA< VectorPolicy< double ca(6);
2.此代码的输出为as如下:
实例化之前
默认StoragePolicy构造函数
参数构造函数
实例化之后ca $>
ClassA打印()
VectorPolicy打印()
实例化cb之前
默认StoragePolicy构造函数
参数构造函数
实例化cb后
我不喜欢用实例化实例化类的想法默认
构造函数,如果我只使用参数构造函数。这是为什么?
如果我删除了默认构造函数,那么我就有编译错误:
main.cpp:在构造函数''ClassA< T,StoragePolicy> :: ClassA (size_t)
[T = double,StoragePolicy = BitSetPolicy]'':
main.cpp:110:从这里实例化
main .cpp:87:错误:没有匹配函数来调用
''BitSetPolicy< double> :: BitSetPolicy()''
main.cpp:60:注意:候选人是:
BitSetPolicy< T> :: BitSetPolicy(size_t)[有T =双]
main.cpp:51:注意:
BitSetPolicy< double> :: BitSetPolicy(const BitSetPolicy< double>&)
我相信两次实例化对象的开销是
not必要,对吗?有没有办法解决这个问题?
3.我还尝试在策略中包含一个create(size_t)函数
返回指针实际存储(矢量或位集)。
然而,编译器给了我转换错误... = /
也许我的一些问题太基本了,所以我大肆宣传,我赞赏你能给予我任何帮助。
亚历杭德罗·阿拉贡
#include < iostream>
#include< vector>
#include< boost / dynamic_bitset.hpp>
使用提升:: dynamic_bitset;
using namespace std;
template< class T>
class StoragePolicy
{
public:
StoragePolicy();
StoragePolicy(size_t size _);
无效打印();
};
模板< class T>
struct VectorPolicy
{
public:
t ypedef std :: vector< TStructure;
VectorPolicy(){
cout<<" default StoragePolicy constructor"<< endl;
}
VectorPolicy(size_t size _){
cout<<""""""<< endl;
str_ =新向量< T>(size_);
}
void print(){
cout<<""><< endl;
}
私人:
结构* str_;
};
模板< class T>
struct BitSetPolicy
{
public:
typedef boost :: dynamic_bitset< Structure;
BitSetPolicy() {
cout<<"" default StoragePolicy构造函数"<< endl;
}
BitSetPolicy(size_t size_ ){
cout<<"""参数构造函数"<< endl;
str_ = new dynamic_bitset<>(size_);
}
void print(){
cout<<"" BitSetPolicy print()"<< endl;
}
私人:
结构* str_;
};
模板
<
类T,
模板< classclass StoragePolicy
>
class ClassA:public StoragePolicy< T>
{
StoragePolicy< T> * structure_;
public:
//参数构造函数
ClassA(size_t size_){
structure_ = new StoragePolicy< T>(size_);
}
//打印功能
无效打印()
{
cout<<"" ClassA print()"<< endl;
structure _-> print ();
}
};
int main()
{
cout<<"在实例化ca"<<<< endl;
ClassA< double,VectorPolicyca(6);
cout<< ;实例化ca之后<<< endl;
ca.print();
cout<< endl;
cout<<"在实例化cb"<<< endl;
ClassA< double,BitSetPolicycb(6);
cout<< 在实例化cb之后<&l t; endl;
返回0;
}
aaragon写道:
大家好。我正在尝试写一个基于策略的设计课程
(Alexandrescu的现代C ++设计)。我不是程序员,而是一名工程师,所以这对我来说很难。通过使用策略,
我想自定义一个类的结构。我们的想法是:b $ b调查几种数据结构的使用。一个选项是使用boost动态bitset
。另一个是使用
std :: vector。我获得了一些正常工作的代码,如下所示。
有些东西我不喜欢代码,这就是我要求的原因。 m发布此消息。
亲爱的aaragon:
i已经清除了你的代码清单。我很害怕
它看起来对我来说你不是c ++程序员。
i并不声称是专业的
而我也不知道Alexanderscue
i不明白该计划的目的是什么
我怀疑你能得到一个满足你期望的节目
但假设你知道你在做什么
i有以下建议:
你写的
:
>模板< class T>
类StoragePolicy
{
公开:
StoragePolicy();
StoragePolicy(size_t size_);
void print();
};
这部分代码看起来毫无意义且容易出错
删除或评论
> 1。在当前版本中,用户必须键入
ClassA< double,VectorPolicyca(6);
来创建ClassA的对象。我想把它更改为
ClassA< VectorPolicy< double ca(6);
您已将课程定义为:
模板
<
class T,
template< classclass StoragePolicy
class ClassA:public StoragePolicy< T>
{
这在ClassA类之前错过了一个''>''并导致编译器不编译代码,b / b
更多
当前状态下代码的这一部分
(添加了前面提到的缺失''>'')
在microsoft平台上编译,但我不确定它
是标准的,可移植到其他平台,例如
borland。因此我同意你更改
代码:
模板
<
class StoragePolicy
>
class ClassA:public StoragePolicy
{
那么你必须输入代码为:
ClassA< VectorPolicy< double ca(6);
但为了拥有更一致的代码
可选:
1.你可以输入
typedef T value_type;
在第一个''{''
$之后b $ b跟''struct VectorPolicy''和''struct BitSetPolicy''
2.如果你做了第一次,请输入
typedef typename StoragePolicy :: value_type value_type;
在第一个''{''
之后''类ClassA''
>
2。此代码的输出如下:
实例化之前默认的StoragePolicy构造函数
参数构造函数
实例化后的类A打印()
VectorPolicy打印()
实例化cb之前
默认的StoragePolicy构造函数
参数构造函数
实例化cb后
我不喜欢用默认的构造函数实例化类的想法
你有写过:
class ClassA:public StoragePolicy< T>
删除或注释掉'':public StoragePolicy< T>''
且输出不包含:
默认的StoragePolicy构造函数
或者如果你把代码更改为我之前写的内容
你可以更改以下内容: br />
ClassA(size_t size_){
to:
ClassA(size_t size _):StoragePolicy(size_){
否则:
ClassA(size_t size _):StoragePolicy< T>(size_){
这不仅会导致删除
默认的StoragePolicy构造函数
来自输出的
但是也用另一个替换它
参数构造函数
aaragon写道:
大家好。我正在尝试写一个基于策略的设计课程
(Alexandrescu的现代C ++设计)。我不是程序员,而是一名工程师,所以这对我来说很难。通过使用策略,
我想自定义一个类的结构。我们的想法是:b $ b调查几种数据结构的使用。一个选项是使用boost动态bitset
。另一个是使用
std :: vector。我获得了一些正常工作的代码,如下所示。
有些东西我不喜欢代码,这就是我要求的原因。发布此消息。
1.在当前版本中,用户必须键入
ClassA< double,VectorPolicyca(6);
来创建ClassA的对象。我想把它更改为
ClassA< VectorPolicy< double ca(6);
为什么?您当前处理它的方式将允许指定默认的
策略。使用建议的语法,你不能独立于
底层类型(例如,double)。
2.输出此代码如下:
实例化之前的确定
默认的StoragePolicy构造函数
参数构造函数
实例化后的实例*
ClassA打印()
VectorPolicy打印()
实例化cb之前
默认的StoragePolicy构造函数
参数构造函数
实例化cb后的
我不喜欢实例化的想法如果我只使用参数构造函数,那么该类具有默认的
构造函数。这是为什么?
如果我删除了默认构造函数,那么我就有编译错误:
main.cpp:在构造函数''ClassA< T,StoragePolicy> :: ClassA (size_t)
[T = double,StoragePolicy = BitSetPolicy]'':
main.cpp:110:从这里实例化
main .cpp:87:错误:没有匹配函数来调用
''BitSetPolicy< double> :: BitSetPolicy()''
main.cpp:60:注意:候选人是:
BitSetPolicy< T> :: BitSetPolicy(size_t)[有T =双]
main.cpp:51:注意:
BitSetPolicy< double> :: BitSetPolicy(const BitSetPolicy< double>&)
我相信两次实例化对象的开销是
not必要,对吗?有没有办法来解决这个问题?
我不太确定,我明白你的意思。但是,我提供的代码
会打印默认的构造函数消息。
3.我还尝试在策略中包含一个创建(size_t)函数
返回指向实际存储的指针(向量或位集)。
然而,编译器给了我转换错误... = /
嗯?
也许我的一些问题太基础了,所以我大肆宣传和我
感谢您给我的任何帮助。
AlejandroAragón
#include< iostream>
#include< vector>
#include< boost / dynamic_bitset.hpp>
使用boost :: dynamic_bitset;
using namespace std;
template< class T>
class StoragePolicy
{
public:
StoragePolicy();
StoragePolicy(size_t size_);
void print();
};
模板< class T>
struct VectorPolicy >
{
public:
typedef std :: vector< TStructure;
VectorPolicy( ){
cout<<"" default StoragePolicy构造函数"<< endl;
}
VectorPolicy(size_t) size _){
cout<<""""""<<< endl;
str_ = new vector< T>(size_);
}
void print(){
cout<<"" VectorPolicy print()"<< endl;
}
私人:
结构* str_;
};
模板< class T>
struct BitSetPolicy
{
public:
typedef boost :: dynamic_bitset<结构;
BitSetPolicy(){
cout<<"默认StoragePolicy构造函数"<< ; ENDL;
}
BitSetPolicy(size_t size _){
cout<<"""参数构造函数"<<< endl;
str_ = new dynamic_bitset<>(size_);
}
void print(){
cout<<"" BitSetPolicy print()"<< endl;
}
private:
结构* str_;
};
模板
<
class T,
template< classclass StoragePolicy
>>
class ClassA:public StoragePolicy< T>
{
StoragePolicy< T> * structure_;
public:
//参数构造函数
ClassA(size_t size_){
structure_ = new StoragePolicy< T>(size_);
}
//打印功能
无效打印()
{
cout<<"" ClassA print()"<< endl;
structure _-> print ();
}
};
int main()
{
cout<<">实例化ca"<<< endl;
ClassA< double,VectorPolicyca(6);
cout<<""实例化ca"<<<<<<<<<<<<<<<<<<<<<<<<<<"&b;&n;<<<<<<
cout<<"在实例化cb"<<< endl;
ClassA< double,BitSetPolicycb(6);
cout<<"实例化cb"<<< endl;
返回0;
}
摆脱所有重复和指示:
#include< iostream>
#include< vector>
#include< boost / dynamic_bitset.hpp>
使用boost :: dynamic_bitset;
using namespace std;
模板< class T>
struct VectorPolicy {
typedef std :: vector< TStructure;
VectorPolicy()
:str_()
{
cout<<"" default StoragePolicy constructor"< ;< endl;
}
VectorPolicy(size_t size_)
:str_(size_)
{
cout<<"""参数构造函数"<< endl;
}
void print( ){
cout<<"""<< endl;
}
私人:
结构str_;
};
模板< class T>
struct BitSetPolicy {
typedef boost :: dynamic_bitset< Structure;
BitSetPolicy(){
cout< <" default StoragePolicy构造函数"<< endl;
}
BitSetPolicy(size_t size_)
:str_ (size_)
{
cout<<"""参数构造函数"<< endl;
}
void print(){
cout<<"" BitSetPolicy print()"<< endl;
}
私人:
结构str_;
};
模板< class T,template< classclass StoragePolicy>
class ClassA:public StoragePolicy< T {
typedef StoragePolicy< Tstorage_base;
>
public:
//参数构造函数
ClassA(size_t size_)
:storage_base(size_)< br $>
{}
//打印功能
无效打印()
{
cout<<"" ClassA print()"<< endl;
storage_base :: print();
}
};
int main(){
cout<<">实例化ca"<<< endl;
ClassA< double,VectorPolicyca(6);
cout<<"实例化ca"<<< endl;
ca. print();
cout<< endl;
cout<<"在实例化cb"<<< endl;
ClassA< double,BitSetPolicycb(6);
cout<<"实例化cb"<<< endl;
返回0;
}
现在,您需要在策略中添加一些方法,这些方法将允许您实际操作
底层容器的内容。
最好
Kai-Uwe Bux
" aaragon" < al ************** @ gmail.comwrote:
大家好。我正在尝试写一个基于策略的设计课程
(Alexandrescu的现代C ++设计)。我不是程序员,而是一名工程师,所以这对我来说很难。通过使用策略,
我想自定义一个类的结构。我们的想法是:b $ b调查几种数据结构的使用。一个选项是使用boost动态bitset
。另一个是使用
std :: vector。我获得了一些正常工作的代码,如下所示。
有些东西我不喜欢代码,这就是我要求的原因。发布此消息。
1.在当前版本中,用户必须键入
ClassA< double,VectorPolicyca(6);
来创建ClassA的对象。我想把它更改为
ClassA< VectorPolicy< double ca(6);
ClassA这两个*都是* StoragePolicy并且包含一个,我怀疑这是你想要的
但是我不知道您提供的代码使用哪种方式
it。以下内容适用于所提供的代码。
template< typename StoragePolicy>
class ClassA
{
StoragePolicy结构; //注意,不需要''new''结构
public:
ClassA(size_t size):结构(大小){}
void print(){
cout<< " ClassA print()\ n";
structure.print();
}
};
我建议您阅读初始化列表。同样。
2.此代码的输出如下:
在实例化之前
默认的StoragePolicy构造函数
参数构造函数
实例化后的
ClassA打印()
VectorPolicy在实例化cb之前打印()
默认StoragePolicy构造函数
参数构造函数
实例化后cb
我不喜欢用默认的
构造函数实例化类的想法,如果我只使用参数构造函数的话。为什么是这样?
因为ClassA源自它的存储策略而且没有初始化
基类对象。
< blockquote class =post_quotes>
我相信两次实例化对象的开销是不必要的b / b
,对吗?有没有办法来解决这个问题?
我无法回答第一个问题,只有你知道是否需要两次实例化对象
。但是如果你不想这样做,请使用我上面提供的
ClassA。
3.我也尝试过在策略中包含一个create(size t)函数
返回指向实际存储的指针(向量或bitset)。
然而,编译器给了我转换错误... = /
Alexandrescu的概念是相当高的水平,而不是我想要的b $ b推荐给新来的人语言。坚持使用动态
绑定。
-
要给我发电子邮件,请输入sheltie在主题中。
Hi everyone. I''m trying to write a class with policy based design
(Alexandrescu''s Modern C++ Design). I''m not a programmer but an
engineer so this is kind of hard for me. Through the use of policies,
I want to customize the structure of a class. The idea is to
investigate the use of several data structures. One option would be
the use of the boost dynamic bitset. Another would be the use of the
std::vector. I obtained some code that is working and is shown below.
There are some things that I don''t like about the code and that is the
reason I''m posting this message.
1. In the current version, the user has to type
ClassA<double,VectorPolicyca(6);
to create an object of ClassA. I would like to change this to
ClassA<VectorPolicy<double ca(6);
2. The output of this code is as follows:
before instantiation of ca
default StoragePolicy constructor
Parameter constructor
after instantiation of ca
ClassA print()
VectorPolicy print()
before instantiation of cb
default StoragePolicy constructor
Parameter constructor
after instantiation of cb
I don''t like the idea of instantiating the class with the default
constructor if I''m using only a parameter constructor. Why is this?
If I remove the default constructor then I have a compiler error:
main.cpp: In constructor ''ClassA<T, StoragePolicy>::ClassA(size_t)
[with T = double, StoragePolicy = BitSetPolicy]'':
main.cpp:110: instantiated from here
main.cpp:87: error: no matching function for call to
''BitSetPolicy<double>::BitSetPolicy()''
main.cpp:60: note: candidates are:
BitSetPolicy<T>::BitSetPolicy(size_t) [with T = double]
main.cpp:51: note:
BitSetPolicy<double>::BitSetPolicy(const BitSetPolicy<double>&)
I believe that instantiating the object twice has an overhead that is
not necessary, right? Is there a way to fix this?
3. I also tried to include in the policies, a create(size_t) function
that returns a pointer to the actual storage (vector or bitset).
However, the compiler gave me conversion errors... =/
Maybe some of my questions are too basic, so I apoligize and I
appreciate any help you can give me.
Alejandro Aragón
#include <iostream>
#include <vector>
#include <boost/dynamic_bitset.hpp>
using boost::dynamic_bitset;
using namespace std;
template <class T>
class StoragePolicy
{
public:
StoragePolicy();
StoragePolicy(size_t size_);
void print();
};
template <class T>
struct VectorPolicy
{
public:
typedef std::vector<TStructure;
VectorPolicy(){
cout<<"default StoragePolicy constructor"<<endl;
}
VectorPolicy(size_t size_){
cout<<"Parameter constructor"<<endl;
str_ = new vector<T>(size_);
}
void print(){
cout<<"VectorPolicy print()"<<endl;
}
private:
Structure* str_;
};
template <class T>
struct BitSetPolicy
{
public:
typedef boost::dynamic_bitset<Structure;
BitSetPolicy(){
cout<<"default StoragePolicy constructor"<<endl;
}
BitSetPolicy(size_t size_){
cout<<"Parameter constructor"<<endl;
str_ = new dynamic_bitset<>(size_);
}
void print(){
cout<<"BitSetPolicy print()"<<endl;
}
private:
Structure* str_;
};
template
<
class T,
template <classclass StoragePolicy
>
class ClassA : public StoragePolicy<T>
{
StoragePolicy<T>* structure_;
public:
// parameter constructor
ClassA(size_t size_) {
structure_ = new StoragePolicy<T>(size_);
}
// print function
void print()
{
cout<<"ClassA print()"<<endl;
structure_->print();
}
};
int main()
{
cout<<"before instantiation of ca"<<endl;
ClassA<double,VectorPolicyca(6);
cout<<"after instantiation of ca"<<endl;
ca.print();
cout<<endl;
cout<<"before instantiation of cb"<<endl;
ClassA<double,BitSetPolicycb(6);
cout<<"after instantiation of cb"<<endl;
return 0;
}
aaragon wrote:Hi everyone. I''m trying to write a class with policy based design
(Alexandrescu''s Modern C++ Design). I''m not a programmer but an
engineer so this is kind of hard for me. Through the use of policies,
I want to customize the structure of a class. The idea is to
investigate the use of several data structures. One option would be
the use of the boost dynamic bitset. Another would be the use of the
std::vector. I obtained some code that is working and is shown below.
There are some things that I don''t like about the code and that is the
reason I''m posting this message.Dear aaragon:
i have veiwed your code listing .i am affraid
it looks to me that you are no c++ programer .
i do not claim to be professional
and i dont know Alexanderscue either
i did not understand what the program is intended to do
and i doubt that you can get a program
that satisfies your expectations
but assuming that you know what you are doing
i have the following recommendations for you:
you wrote:>template <class T>
class StoragePolicy
{
public:
StoragePolicy();
StoragePolicy(size_t size_);
void print();
};this portion of the code looks meaningless and error prone
remove or comment it out
>1. In the current version, the user has to type
ClassA<double,VectorPolicyca(6);
to create an object of ClassA. I would like to change this to
ClassA<VectorPolicy<double ca(6);
you have defined the class as:
template
<
class T,
template <classclass StoragePolicy
class ClassA : public StoragePolicy<T>
{
this misses a ''>'' before ''class ClassA'' and causes
the compiler not to compile the code, further more
this fraction of the code in its current state
(after adding the afformentioned missing ''>'')
compiles on microsoft platform but i am not sure that it
is standard and portable to other platforms such as
borland. therefore i agree with you in changing
the code to this:
template
<
class StoragePolicy>class ClassA : public StoragePolicy
{
then you have to type the code as:
ClassA<VectorPolicy<double ca(6);
but in order to have a more consistent code
optionally:
1. you can type
typedef T value_type;
after the first ''{''
following ''struct VectorPolicy'' and ''struct BitSetPolicy''
2.if and only if you did the first ,type
typedef typename StoragePolicy::value_type value_type;
after the first ''{''
following ''class ClassA''
>
2. The output of this code is as follows:
before instantiation of ca
default StoragePolicy constructor
Parameter constructor
after instantiation of ca
ClassA print()
VectorPolicy print()
before instantiation of cb
default StoragePolicy constructor
Parameter constructor
after instantiation of cb
I don''t like the idea of instantiating the class with the default
constructoryou have Writen:
class ClassA : public StoragePolicy<T>
remove or comment out '' : public StoragePolicy<T>''
and the output will not contain:
default StoragePolicy constructor
alternatively if you have changed the code to what i wrote formerly
you can change the following:
ClassA(size_t size_) {
to:
ClassA(size_t size_):StoragePolicy(size_) {
otherwise to:
ClassA(size_t size_):StoragePolicy<T>(size_) {
this will lead to not only removing
default StoragePolicy constructor
from the output but also replacing it with another
Parameter constructor
aaragon wrote:
Hi everyone. I''m trying to write a class with policy based design
(Alexandrescu''s Modern C++ Design). I''m not a programmer but an
engineer so this is kind of hard for me. Through the use of policies,
I want to customize the structure of a class. The idea is to
investigate the use of several data structures. One option would be
the use of the boost dynamic bitset. Another would be the use of the
std::vector. I obtained some code that is working and is shown below.
There are some things that I don''t like about the code and that is the
reason I''m posting this message.
1. In the current version, the user has to type
ClassA<double,VectorPolicyca(6);
to create an object of ClassA. I would like to change this to
ClassA<VectorPolicy<double ca(6);Why? The way you handle it currently would allow for specifying a default
policy. With the proposed syntax, you could not do that independent of the
underlying type (e.g., double).
2. The output of this code is as follows:
before instantiation of ca
default StoragePolicy constructor
Parameter constructor
after instantiation of ca
ClassA print()
VectorPolicy print()
before instantiation of cb
default StoragePolicy constructor
Parameter constructor
after instantiation of cb
I don''t like the idea of instantiating the class with the default
constructor if I''m using only a parameter constructor. Why is this?
If I remove the default constructor then I have a compiler error:
main.cpp: In constructor ''ClassA<T, StoragePolicy>::ClassA(size_t)
[with T = double, StoragePolicy = BitSetPolicy]'':
main.cpp:110: instantiated from here
main.cpp:87: error: no matching function for call to
''BitSetPolicy<double>::BitSetPolicy()''
main.cpp:60: note: candidates are:
BitSetPolicy<T>::BitSetPolicy(size_t) [with T = double]
main.cpp:51: note:
BitSetPolicy<double>::BitSetPolicy(const BitSetPolicy<double>&)
I believe that instantiating the object twice has an overhead that is
not necessary, right? Is there a way to fix this?I am not quite sure, I understand what you mean. However, the code I provide
below does print default constructor messages.
3. I also tried to include in the policies, a create(size_t) function
that returns a pointer to the actual storage (vector or bitset).
However, the compiler gave me conversion errors... =/Huh?
Maybe some of my questions are too basic, so I apoligize and I
appreciate any help you can give me.
Alejandro Aragón
#include <iostream>
#include <vector>
#include <boost/dynamic_bitset.hpp>
using boost::dynamic_bitset;
using namespace std;
template <class T>
class StoragePolicy
{
public:
StoragePolicy();
StoragePolicy(size_t size_);
void print();
};
template <class T>
struct VectorPolicy
{
public:
typedef std::vector<TStructure;
VectorPolicy(){
cout<<"default StoragePolicy constructor"<<endl;
}
VectorPolicy(size_t size_){
cout<<"Parameter constructor"<<endl;
str_ = new vector<T>(size_);
}
void print(){
cout<<"VectorPolicy print()"<<endl;
}
private:
Structure* str_;
};
template <class T>
struct BitSetPolicy
{
public:
typedef boost::dynamic_bitset<Structure;
BitSetPolicy(){
cout<<"default StoragePolicy constructor"<<endl;
}
BitSetPolicy(size_t size_){
cout<<"Parameter constructor"<<endl;
str_ = new dynamic_bitset<>(size_);
}
void print(){
cout<<"BitSetPolicy print()"<<endl;
}
private:
Structure* str_;
};
template
<
class T,
template <classclass StoragePolicy>>class ClassA : public StoragePolicy<T>
{
StoragePolicy<T>* structure_;
public:
// parameter constructor
ClassA(size_t size_) {
structure_ = new StoragePolicy<T>(size_);
}
// print function
void print()
{
cout<<"ClassA print()"<<endl;
structure_->print();
}
};
int main()
{
cout<<"before instantiation of ca"<<endl;
ClassA<double,VectorPolicyca(6);
cout<<"after instantiation of ca"<<endl;
ca.print();
cout<<endl;
cout<<"before instantiation of cb"<<endl;
ClassA<double,BitSetPolicycb(6);
cout<<"after instantiation of cb"<<endl;
return 0;
}Get rid of all the duplicates and pointers:
#include <iostream>
#include <vector>
#include <boost/dynamic_bitset.hpp>
using boost::dynamic_bitset;
using namespace std;
template <class T>
struct VectorPolicy {
typedef std::vector<TStructure;
VectorPolicy()
: str_ ()
{
cout<<"default StoragePolicy constructor"<<endl;
}
VectorPolicy (size_t size_)
: str_ ( size_ )
{
cout<<"Parameter constructor"<<endl;
}
void print(){
cout<<"VectorPolicy print()"<<endl;
}
private:
Structure str_;
};
template <class T>
struct BitSetPolicy {
typedef boost::dynamic_bitset<Structure;
BitSetPolicy(){
cout<<"default StoragePolicy constructor"<<endl;
}
BitSetPolicy(size_t size_)
: str_ ( size_ )
{
cout<<"Parameter constructor"<<endl;
}
void print(){
cout<<"BitSetPolicy print()"<<endl;
}
private:
Structure str_;
};
template < class T, template <classclass StoragePolicy >
class ClassA : public StoragePolicy<T{
typedef StoragePolicy<Tstorage_base;
public:
// parameter constructor
ClassA(size_t size_)
: storage_base ( size_ )
{}
// print function
void print()
{
cout<<"ClassA print()"<<endl;
storage_base::print();
}
};
int main(){
cout<<"before instantiation of ca"<<endl;
ClassA<double,VectorPolicyca(6);
cout<<"after instantiation of ca"<<endl;
ca.print();
cout<<endl;
cout<<"before instantiation of cb"<<endl;
ClassA<double,BitSetPolicycb(6);
cout<<"after instantiation of cb"<<endl;
return 0;
}
Now, you would need to add some methods to your policies that will allow you
to actually manipulate the contents of the underlying container.
Best
Kai-Uwe Bux
"aaragon" <al**************@gmail.comwrote:
Hi everyone. I''m trying to write a class with policy based design
(Alexandrescu''s Modern C++ Design). I''m not a programmer but an
engineer so this is kind of hard for me. Through the use of policies,
I want to customize the structure of a class. The idea is to
investigate the use of several data structures. One option would be
the use of the boost dynamic bitset. Another would be the use of the
std::vector. I obtained some code that is working and is shown below.
There are some things that I don''t like about the code and that is the
reason I''m posting this message.
1. In the current version, the user has to type
ClassA<double,VectorPolicyca(6);
to create an object of ClassA. I would like to change this to
ClassA<VectorPolicy<double ca(6);ClassA both *is* a StoragePolicy and contains one, I doubt this is what
you want but I don''t know from the code you present which way to go with
it. The following works with the code presented though.
template < typename StoragePolicy >
class ClassA
{
StoragePolicy structure; // note, no need to ''new'' the structure
public:
ClassA( size_t size ): structure( size ) { }
void print() {
cout << "ClassA print()\n";
structure.print();
}
};
I suggest you read up on "initialization lists" as well.
2. The output of this code is as follows:
before instantiation of ca
default StoragePolicy constructor
Parameter constructor
after instantiation of ca
ClassA print()
VectorPolicy print()
before instantiation of cb
default StoragePolicy constructor
Parameter constructor
after instantiation of cb
I don''t like the idea of instantiating the class with the default
constructor if I''m using only a parameter constructor. Why is this?Because ClassA derives from it''s storage policy and doesn''t initialize
the base class object.
I believe that instantiating the object twice has an overhead that is
not necessary, right? Is there a way to fix this?I can''t answer the first question, only you know if it''s necessary to
instantiate the object twice. But if you don''t want to do so, use the
ClassA that I provided above.
3. I also tried to include in the policies, a create(size t) function
that returns a pointer to the actual storage (vector or bitset).
However, the compiler gave me conversion errors... =/Alexandrescu''s concepts are quite high level, not something I would
recommend to a person new to the language. Stick to using dynamic
binding.
--
To send me email, put "sheltie" in the subject.
这篇关于使用策略类自定义结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!