473,322 Members | 1,523 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 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 7769
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Jonan | last post by:
Hello, For several reasons I want to replace the built-in memory management with some custom built. The mem management itlsef is not subject to my question - it's ok to the point that I have...
3
by: Sensei | last post by:
Hi. I have a problem with a C++ code I can't resolve, or better, I can't see what the problem should be! Here's an excerpt of the incriminated code: === bspalgo.cpp // THAT'S THE BAD...
2
by: Tony Johansson | last post by:
Hello Experts! I have one class template here called Handle and one concrete class called Point. At the bottom is the main program and the class template definitions for Handle. This program...
1
by: Tony Johansson | last post by:
This class template and main works perfectly fine. But could be better. I have this class template called Handle that has a pointer declared as T* body; As you can see I have a reference counter...
2
by: Shark | last post by:
Hi, if we need to change the behavior of operator new, it is called overriding or overloading? My other question is, if we change the behavior of operator new, do we use malloc to do that or we use...
5
by: tom | last post by:
Hi, I'm overriding my operator new and operator delete for two classes, one inherited from the other, so I can use my own memory pool. A simplified version of what I have is below: class...
1
by: amhoov | last post by:
Hi all, OS - MacOS X 10.4.9 Compiler - gcc 4.0 (XCode) I'm *extremely* new to C++ (but have extensive experience with Java), so please go easy on me. I'm running into a very odd situation...
14
by: Hunk | last post by:
Hi I ws wondering if there is a way to implement operator+ in case of virtual classes. Here's the problem. I have to have a base string class from which two classes (normal char string and a...
9
by: wo3kie | last post by:
#include <iostream> #include <map> #include <utility> // // Base // / | \ // Derived1 Derived2 \ // \ | / // Derived3
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.