, , operator new, . g++ 4.0.1 ( , "", ):
#include <cstddef>
template <typename T>
class test {
public:
std::size_t len;
T arr[1];
void *operator new(std::size_t s, std::size_t a);
test(const T& f) { fill(f); }
test();
private:
void fill(const T& f) { for(std::size_t i = 0; i < len; i++) arr[i] = f; }
};
template <typename T>
void *test<T>::operator new(std::size_t s, std::size_t a)
{
void *p = ::operator new(s + (a - 1) * sizeof(T));
((test<T> *)p)->len = a;
return p;
}
:
#include <iostream>
int main()
{
test<char> *c = new (10) test<char>('c');
std::cout << c->arr[3] << std::endl;
delete c;
return 0;
}
new.