A
aaragon
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,VectorPolicy> ca(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<T> Structure;
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,
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,VectorPolicy> ca(6);
cout<<"after instantiation of ca"<<endl;
ca.print();
cout<<endl;
cout<<"before instantiation of cb"<<endl;
ClassA<double,BitSetPolicy> cb(6);
cout<<"after instantiation of cb"<<endl;
return 0;
}
(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,VectorPolicy> ca(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<T> Structure;
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,
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,VectorPolicy> ca(6);
cout<<"after instantiation of ca"<<endl;
ca.print();
cout<<endl;
cout<<"before instantiation of cb"<<endl;
ClassA<double,BitSetPolicy> cb(6);
cout<<"after instantiation of cb"<<endl;
return 0;
}