473,388 Members | 1,970 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,388 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 7775
"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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.