472,117 Members | 2,451 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,117 software developers and data experts.

operator new & delete overriding in template

When overriding operator new & delte of one class, the method is
implicitly declared as static. However, overriding operator new & delete
of template cannot be static.The compiler says cannot declare the
function a static linkage. Why?
C++ is so complex and so many situation should be considered.
Jul 22 '05 #1
3 7674
"Cheng Mo" <mo******@nospam.nospam> wrote...
When overriding operator new & delte of one class, the method is
implicitly declared as static. However, overriding operator new & delete
of template cannot be static.The compiler says cannot declare the function
a static linkage. Why?
C++ is so complex and so many situation should be considered.


Read the FAQ 5.8.

This:
--------------------
#include <typeinfo>
#include <iostream>

template<class T>
class A {
public:
A(int) {}
void * operator new(size_t s) {
std::cout << "new A<" << typeid(T).name() << ">\n";
return malloc(s); }
void operator delete(void* p, size_t s) { free(p); }
};

int main()
{
A<int> *paint = new A<int>(42);
}
--------------------
compiles just fine (as it should)

V
Jul 22 '05 #2
I know that the compilation and execution is OK.
What I want to konw is that why member functions in template cannot be
declared as static.

Victor Bazarov wrote:
"Cheng Mo" <mo******@nospam.nospam> wrote...
When overriding operator new & delte of one class, the method is
implicitly declared as static. However, overriding operator new & delete
of template cannot be static.The compiler says cannot declare the function
a static linkage. Why?
C++ is so complex and so many situation should be considered.

Read the FAQ 5.8.

This:
--------------------
#include <typeinfo>
#include <iostream>

template<class T>
class A {
public:
A(int) {}
void * operator new(size_t s) {
std::cout << "new A<" << typeid(T).name() << ">\n";
return malloc(s); }
void operator delete(void* p, size_t s) { free(p); }
};

int main()
{
A<int> *paint = new A<int>(42);
}
--------------------
compiles just fine (as it should)

V

Jul 22 '05 #3
Cheng Mo wrote:
I know that the compilation and execution is OK.
What I want to konw is that why member functions in template cannot be
declared as static.
I don't understand the question, probably. Overloaded operators 'new' and
'delete' shall not be _declared_ static, but they _are_ static, as you
noted in your post. Any other function _can_ be declared static, unless
it's a constructor, the destructor, operator= (and some other operators).

What makes you think that members of templates can't be static?

template<class T> class HasStatic {
public:
static void foo(); // here it is, a static member
};

And, please, don't top-post, OK? Thanks!

Victor Bazarov wrote:
"Cheng Mo" <mo******@nospam.nospam> wrote...
When overriding operator new & delte of one class, the method is
implicitly declared as static. However, overriding operator new &
delete of template cannot be static.The compiler says cannot declare
the function a static linkage. Why?
C++ is so complex and so many situation should be considered.


Read the FAQ 5.8.

This:
--------------------
#include <typeinfo>
#include <iostream>

template<class T>
class A {
public:
A(int) {}
void * operator new(size_t s) {
std::cout << "new A<" << typeid(T).name() << ">\n";
return malloc(s); }
void operator delete(void* p, size_t s) { free(p); }
};

int main()
{
A<int> *paint = new A<int>(42);
}
--------------------
compiles just fine (as it should)

V

--
Please remove capital As from my address when replying by mail
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Sensei | last post: by
2 posts views Thread by Tony Johansson | last post: by
1 post views Thread by Tony Johansson | last post: by
2 posts views Thread by Shark | last post: by
1 post views Thread by amhoov | last post: by
14 posts views Thread by Hunk | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.