2013-03-14 134 views
3

您能解釋下面代碼的輸出嗎?我需要做什麼來調用正確的基類構造函數?派生類調用錯誤的基類構造函數

謝謝。

#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) 
... 
+4

請參閱:http://stackoverflow.com/questions/2126522/c-virtual-inheritance – 2013-03-14 11:16:02

+2

你期望什麼? – 2013-03-14 11:16:13

+0

爲什麼你甚至使用虛擬繼承? – Grizzly 2013-03-14 11:17:06

回答

2

虛擬基類是從最派生類型的構造器進行初始化。因此在前兩個示例中,調用SequenceComposite的默認構造函數。這是一個allocator_type與默認參數。要使用不同的構造函數,請從最大派生類型的初始化程序列表中調用它。

相關問題