allocating memory for derived class members based on boost smart pointers in the base class through CRTP
===

**///////This part is to provide some background info and can be skipped///////**

I am working on a template library which heavily relies on the use of the curiously recurring template pattern (primarily for static polymorphism). The idea is that to use it the user can either

1). Use predefined classes with standard methods, which are very simple leafs of the base class that only provide constructors/destructor, declare the variable members and declare the base class(es) as friend(s). All actual methods that operate on the variable members of the derived classes are defined in the base class(es).
 
2). Use the base classes to create his/her own extensions, which are very much alike the class described in 1)., but, obviously, this method also allows to add user defined methods that operate on the same variable members (in addition to the ones defined in the base class). 

Whatever method the user chooses he only uses one level extension of the base class. 

My question is primarily about the point 2). In the current implementation the user (and also the developer of the "standard" leafs) has to define all constructors implicitly (i.e. describe full process of memory allocation for dynamic variable members of the class, etc). 

**///////This part is to provide some background info and can be skipped///////**

Today, I decided to investigate my idea to use CRTP/static polymorphism in constructors of the base class to provide definition of the allocation of the memory of all heap variables of the derived class. I did some experiments on sample classes that I created for this exercise.

This is part of the base class 


    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().MyPoint << std::endl;
    	asLeaf().Point=new MyClass(MyClassInstance);
    	std::cout << asLeaf().MyPoint << std::endl;
    }

    TLeafType& asLeaf(void) {
    return static_cast<TLeafType&>(*this);
    }

    ...
    };

Then the derived class:

    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;
        }
    ...
    }

Then in main 

	

    int a(5);
    sysDiscreteTrajectoryPoint<int> A(a);


As expected, it gave the following output:

    Base additional constructor called
    0x847ff4
    0x8737008
    Derived additional constructor called 
    0x8737008
    5
    Derived destructor called 
    Base destructor called 


This suggests that, overall, the concept may be feasible. However, my first question is still about whether or not I fully understand all processes that happen here. I am primarily interested in the efficiency of the whole process, as I will need to be creating a substantial amount of the objects of the class described above and I am wondering what exactly happens with "Point" (are there no hidden redefinitions of where it points?)

The second question is regarding the use of the boost library for the definition of smart pointer for the variable member of the derived class. When I tried replacing the normal pointer with boost::shared_ptr, I received a segmentation fault error when trying to allocate memory for the member of the derived class through base class. 

I present the important parts of the code that I used below:

This is part of the base class 


    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().MyPoint << std::endl;
    	asLeaf().Point.reset(new MyClass(MyClassInstance));
    	std::cout << asLeaf().MyPoint << std::endl;
    }

    TLeafType& asLeaf(void) {
    return static_cast<TLeafType&>(*this);
    }

    ...
    };

Then the derived class:

    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;
        }
    ...
    }

Then in main 	

    int a(5);
    sysDiscreteTrajectoryPoint<int> A(a);


This gives the following output:

    Base additional constructor called
    0x28d324
    Segmentation fault

I have also tried scoped_ptr and it also failed at the run time but with invalid pointer error:

    Base additional constructor called
    *** glibc detected *** ./TestSystem: free(): invalid pointer: 0x00d3fff4 ***
    ======= Backtrace: =========
    /lib/i386-linux-gnu/libc.so.6(+0x6b961)[0xc4e961]
    ...


I guess it is somehow related to the specifics of the operation of boost smart pointers. Does anyone know how to resolve this issue? 






