Re: no response from allocator
Sepidar wrote:
:: hi all,
:: I'm trying to write a custom allocator for myself, using with
:: std::jvector. But it seems that I have no response from it. See the
:: code:
::
:: #include <iostream>
:: #include <vector>
:: #include <memory>
::
:: using std::cout;
:: using std::endl;
::
:: class MyClass
:: {
:: public:
:: MyClass(){cout<<"a new instance of MyClass created!"<<endl;}
:: ~MyClass(){cout<<"one instance of MyClass destroyd!"<<endl;}
:: };
:: typedef MyClass* MyClassPointer;
::
:: class MyAllocator:public std::allocator<MyClassPointer>
:: {
:: public:
:: MyAllocator()
:: {
:: cout<<"message from MyAllocator::MyAllocator"<<endl;
:: }
:: void destroy(pointer p)
:: {
:: cout<<"message from MyAllocator::destroy"<<endl;
:: //whatever
:: //std::allocator<MyClassPointer>::destroy(p);
:: }
:: void deallocate(pointer p, size_type n)
:: {
:: cout<<"message from MyAllocator::deallocate"<<endl;
:: //whatever
:: //std::allocator<MyClassPointer>::deallocate(p,n);
:: }
::
:: };
::
:: typedef std::vector<MyClassPointer,MyAllocator> MyVector;
::
:: int main()
:: {
:: MyVector vec;
::
:: vec.push_back(new MyClass());
::
:: vec.pop_back(); //free memory and print someting on screen
:: vec.clear(); //please do it!
::
:: return 0;
:: }
::
:: It is expected that the code return at least message from
:: MyAllocator's constructor, but there is nothing showing that.
:: Please help.
I get this result:
message from MyAllocator::MyAllocator
a new instance of MyClass created!
message from MyAllocator::destroy
message from MyAllocator::deallocate
Seems ok to me.
Bo Persson