次のコードの出力について説明していただけますか?そして、適切な基本クラスのコンストラクターを呼び出すために何をする必要がありますか?
ありがとう。
#include <vector>
#include <iostream>
template <class CONTAINER> class SequenceComposite {
protected:
CONTAINER m_data;
public:
typedef typename CONTAINER::value_type value_type;
typedef typename CONTAINER::allocator_type allocator_type;
typedef typename CONTAINER::size_type size_type;
explicit SequenceComposite(const allocator_type& alloc = allocator_type()) : m_data(alloc) {
std::cout << std::endl << "SequenceComposite(alloc)" << std::endl;
}
explicit SequenceComposite(size_type n, const value_type& val = value_type(),
const allocator_type& alloc = allocator_type()) : m_data(n, val, alloc) {
std::cout << std::endl << "SequenceComposite(n, val, alloc)" << std::endl;
}
SequenceComposite(const SequenceComposite& x) : m_data(x.m_data) {
std::cout << std::endl << "SequenceComposite(x)" << std::endl;
}
};
template <class DTYPE>
class VectorComposite : public virtual SequenceComposite< std::vector<DTYPE> > {
public:
typedef typename VectorComposite::value_type value_type;
typedef typename VectorComposite::allocator_type allocator_type;
typedef typename VectorComposite::size_type size_type;
explicit VectorComposite(const allocator_type& alloc = allocator_type()) : SequenceComposite< std::vector<DTYPE> >(alloc) {
std::cout << "VectorComposite(alloc)" << std::endl;
}
explicit VectorComposite(size_type n, const value_type& val = value_type(),
const allocator_type& alloc = allocator_type()) : SequenceComposite< std::vector<DTYPE> >(n, val, alloc) {
std::cout << "VectorComposite(n, val, alloc)" << std::endl;
}
VectorComposite(const VectorComposite& x) : SequenceComposite< std::vector<DTYPE> >(x) {
std::cout << "VectorComposite(x)" << std::endl;
}
};
template<typename T> class MyModel : public virtual VectorComposite<T> {
public:
MyModel() {
std::cout << "MyModel()" << std::endl;
};
MyModel(const MyModel<T> &vec) : VectorComposite<T>(vec) {
std::cout << "MyModel(x)" << std::endl;
}
MyModel( size_t n, const T& value= 0) : VectorComposite<T>(n, value) {
std::cout << "MyModel(n, val)" << std::endl;
}
};
int main() {
MyModel<float> c(4, 2.0);
MyModel<float> e(c);
VectorComposite<float> a(3, 2.0);
VectorComposite<float> b(c);
return 0;
}
出力:
SequenceComposite(alloc)
VectorComposite(n, val, alloc)
MyModel(n, val)
SequenceComposite(alloc)
VectorComposite(x)
MyModel(x)
SequenceComposite(n, val, alloc)
VectorComposite(n, val, alloc)
SequenceComposite(x)
VectorComposite(x)
期待していた
SequenceComposite(n, val, alloc)
VectorComposite(n, val, alloc)
MyModel(n, val)
SequenceComposite(x)
VectorComposite(x)
MyModel(x)
...