Re: General Allocator Regarding type definitions and void * specialized
problem
* PeterAPIIT@gmail.com:
This is all my code.
Uhm, please specify the problem in the article's text, not only in the subject line.
Subject line: "General Allocator Regarding type definitions and void *
specialized problem"
It would also be good with a little more detail about what the problem is. :-)
[code]
#ifndef _Custom_Allocator_
#define _Custom_Allocator_
Identifier starting with underscore followed by uppercase is reserved for the
implementation, don't use.
Also, preferentially use ALL UPPERCASE for macros (and generally not for other
identifiers, except where idiomatic such as T for template parameter).
#include <memory>
using std::allocator;
Don't use 'using' in a header file.
You're forcing client code to deal with name clashes.
// ================================================
template <class T>
class MyAllocator
{
public:
// Type Definitions
typedef T value_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef size_t size_type; // Unsigned
// Signed
typedef ptrdiff_t difference_type;
// Member Function
MyAllocator(){}
// MyAllocator(const MyAllocator<T> &rhs);
~MyAllocator(){}
// MyAllocator<T>::rebind<U>::other;
template <class U>
struct rebind
{
typedef MyAllocator<U> other;
}
Missing semicolon.
Which means: this is not your actual code.
At least not a version that you've managed to compile.
pointer address(reference memory) const
{
return *memory;
}
Invalid operation.
Which means: this is not your actual code.
const_pointer address(const_reference memory) const
{
return *memory;
}
Invalid operation.
Which means: this is not your actual code.
size_type MaxMemory()
{
return ;
}
Invalid operation.
Which means: this is not your actual code.
pointer allocate(size_type allocateSiZe)
{
return ::operator new (allocateSize);
}
Misspelled name.
Which means: this is not your actual code.
void deallocate(pointer aPtr, size_type allocateSize)
{
if (aPtr != 0)
{
destrory(aPtr);
::operator delete aPtr[allocateSize];
}
}
Misspelled name.
Which means: this is not your actual code.
void construct(pointer aPtr, const_reference value)
{
if (aPtr != 0)
{
// ((void *)aPtr) is placement new
// T(value) convert to T type
::operator new ((void *)aPtr) T(value);
}
}
Invalid syntax.
Which means: this is not your actual code.
void destrory(pointer aPtr)
{
if (aPtr != 0)
{
*aPtr = 0;
}
}
Misspelled name.
Which means: this is not your actual code.
};
/*
No refernce type to void* -That's why need
specialization for void.
*/
// ============== Global Functions ===============
template<class T1, class T2>
bool operator==(MyAllocator<T1>, MyAllocator<T2>) const
{
return MyAllocator<T1> == MyAllocator<T2>;
}
Invalid operation.
Which means: this is not your actual code.
template<class T1, class T2>
bool operator!=(MyAlloc<T1>, MyAlloc<T2>) const
{
return MyAllocator<T1> != MyAllocator<T2>;
}
Invalid operation.
Which means: this is not your actual code.
// ================================================
#endif
[/code]
What should i do for next steps ?
You should stop thinking about defining your own allocator.
Instead you should start with basics, learning C++ syntax and basic programming
(expressions, loops, decisions, routines). A good C++ textbook will help with
that. E.g. Glasborrow's "You can do it!", or, if you already know some
programming (perhaps in some other language), "Accelerated C++".
And when you run into some problem then, please post actual code that compiles.
Cheers & hth.,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?