質問のこの部分は背景情報を提供するものであり、無視できます
私は、不思議なことに繰り返されるテンプレート パターンの使用に大きく依存するテンプレート ライブラリに取り組んでいます。クラス構造の背後にある考え方は、ユーザーが次のいずれかを実行できるということです。
1)。標準メソッドで事前定義されたクラスを使用します。これらのクラスは、コンストラクタ/デストラクタのみを提供し、変数メンバーを宣言し、基本クラスをフレンドとして宣言する基本クラスの非常に単純なリーフです。派生クラスの変数メンバーを操作するすべてのメソッドは、基本クラスで定義されます。
2)。基本クラスを使用して、独自の拡張機能を作成します。このメソッドにより、ユーザーは同じ変数メンバーで動作する独自のメソッドを導入することもできます。
設計により、単一レベルの継承のみが適用されます。
私の質問は、主に節 2 に関するものです。現在の実装では、ユーザーはすべてのコンストラクターを暗黙的に定義する必要があります (つまり、クラスの動的変数メンバーのメモリ割り当ての完全なプロセスを記述するなど)。
質問
以下の例は、CRTP を使用して、基本クラス コンストラクター内の派生クラスのヒープ変数のメモリ割り当ての定義を提供する可能性についての調査を示しています。
基本クラスの一部
template<class TLeafType, class MyClass> class sysBaseDiscreteTrajectoryPoint {
...
//one of the base constructors
sysBaseDiscreteTrajectoryPoint(const MyClass& MyClassInstance) {
std::cout << "Base additional constructor called" << std::endl;
std::cout << asLeaf().Point << std::endl;
asLeaf().Point=new MyClass(MyClassInstance);
std::cout << asLeaf().Point << std::endl;
}
TLeafType& asLeaf(void) {
return static_cast<TLeafType&>(*this);
}
...
};
派生クラス:
template<class MyClass>
class sysDiscreteTrajectoryPoint: public sysBaseDiscreteTrajectoryPoint<sysDiscreteTrajectoryPoint<MyClass>, MyClass> {
...
friend class sysBaseDiscreteTrajectoryPoint<sysDiscreteTrajectoryPoint<MyClass>, MyClass>;
private:
MyClass* Point;
public:
sysDiscreteTrajectoryPoint(const MyClass& MyClassInstance): sysBaseDiscreteTrajectoryPoint<sysDiscreteTrajectoryPoint<MyClass>, MyClass>(MyClassInstance){
std::cout << "Derived additional constructor called " << std::endl;
std::cout << Point << std::endl;
std::cout << *Point << std::endl;
}
...
}
主要:
int a(5);
sysDiscreteTrajectoryPoint<int> A(a);
コードは次の出力を生成します。
Base additional constructor called
0x847ff4
0x8737008
Derived additional constructor called
0x8737008
5
Derived destructor called
Base destructor called
出力は、コンセプトが実現可能である可能性があることを示唆しています。ただし、2 つの質問があります。
1)。コードの実行中に発生するすべてのプロセスを理解していることを確認したいと思います。特に、上記のクラスからかなりの量のオブジェクトをインスタンス化する必要がある可能性があるため、プロセスの効率に関心があり、何が起こるかを理解したいPoint
(隠された再定義はありますか?)
2)。boost
質問は、派生クラスのメンバーのスマート ポインターを定義するためのライブラリの使用に関連しています。raw ポインターを に置き換えようとboost::shared_ptr
したときに、基本クラスを介して派生クラスのメンバーにメモリを割り当てようとすると、セグメンテーション フォールト エラーが発生しました。コードの重要なセクションを以下に示します。
基本クラスの一部:
template<class TLeafType, class MyClass> class sysBaseDiscreteTrajectoryPoint {
...
//one of the base constructors
sysBaseDiscreteTrajectoryPoint(const MyClass& MyClassInstance) {
std::cout << "Base additional constructor called" << std::endl;
std::cout << asLeaf().Point << std::endl;
asLeaf().Point.reset(new MyClass(MyClassInstance));
std::cout << asLeaf().Point << std::endl;
}
TLeafType& asLeaf(void) {
return static_cast<TLeafType&>(*this);
}
...
};
派生クラスの一部:
template<class MyClass>
class sysDiscreteTrajectoryPoint: public sysBaseDiscreteTrajectoryPoint<sysDiscreteTrajectoryPoint<MyClass>, MyClass> {
...
friend class sysBaseDiscreteTrajectoryPoint<sysDiscreteTrajectoryPoint<MyClass>, MyClass>;
private:
boost::shared_ptr<MyClass> Point;
public:
sysDiscreteTrajectoryPoint(const MyClass& MyClassInstance): sysBaseDiscreteTrajectoryPoint<sysDiscreteTrajectoryPoint<MyClass>, MyClass>(MyClassInstance){
std::cout << "Derived additional constructor called " << std::endl;
std::cout << Point << std::endl;
std::cout << *Point << std::endl;
}
...
}
主要:
int a(5);
sysDiscreteTrajectoryPoint<int> A(a);
コードは次の出力を生成します。
Base additional constructor called
0x28d324
Segmentation fault
私も試しscoped_ptr
ました。ただし、実行時に失敗しましたが、別のエラーが発生しました:
Base additional constructor called
*** glibc detected *** ./TestSystem: free(): invalid pointer: 0x00d3fff4 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x6b961)[0xc4e961]
...
ブーストスマートポインターの動作の詳細に関連していると思います。この問題を解決する方法を知っている人はいますか?