473,406 Members | 2,378 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,406 software developers and data experts.

operator new and constructor argument

Hi
I'm in trouble with operator new:
I have overloaded it to track allocated memory, but what I really need
is to get constructor argument.

/////////////CODE:
class myClass {
public:
myClass(int n) { p = new int[n];}:
~myClass() {delete []p;}

int* p;

void* operator new( size_t stAllocateBlock );
};
/////////////ENDCODE

Then I call my own operator new:

myClass* point = new myClass ( 10 );

What I want is to know constructor argument inside operator new.
Is it possible or it's just a dream?

please answer me. Thanks

Feb 8 '07 #1
1 2368
me**********@virgilio.it wrote:
I'm in trouble with operator new:
I have overloaded it to track allocated memory, but what I really need
is to get constructor argument.

/////////////CODE:
class myClass {
public:
myClass(int n) { p = new int[n];}:
~myClass() {delete []p;}

int* p;

void* operator new( size_t stAllocateBlock );
};
/////////////ENDCODE

Then I call my own operator new:

myClass* point = new myClass ( 10 );

What I want is to know constructor argument inside operator new.
Is it possible or it's just a dream?
operator new doesn't see the constructor argument. Its job is to
allocate memory, and all it needs for that is the size of the thing that
it's allocating. You can define it to take additional arguments, though,
like this:

void *operator new(size_t size, int count);

and now when it's called you can do whatever you need to do with count.
But you have to use the somewhat funky placement syntax:

myClass *point = new (10) myClass(10);

Having to write that 10 twice is not a good idea, so you should add a
layer of indirection:

So what you need is an additional layer of indirection:

myClass *allocate_myClass(int n)
{
return new (n) myClass(n);
}

And now use that instead:

myClass *point = allocate_myClass(10);

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Feb 8 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: David Williams | last post by:
Hi all, I have a templated Vector3D class which holds (x,y,z) components as the specified type. I quite often wish to cast a Vector3D holding ints into a Vector3D holding floats and vice versa....
17
by: Ashwin | last post by:
hi guys, i have overloaded the << operator.as shown below. ostream& operator<<(ostream &out, const student &a) { out<<a.idno; out<< " " ; // out<< a.name; out<< " " ; // out<< a.marks...
3
by: Ganesh Rajaraman | last post by:
Hi, This is the program that i am trying. class A { public: A() { cout<<"Default Constructor"<<endl;
5
by: raan | last post by:
What I am trying to achieve here is depicted in the small program below. // Wrapit.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <map> #include...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
5
by: vairavans | last post by:
Hi Everyone, I have the following code, class B { }; class A {
30
by: none | last post by:
I'm trying to overload the = operator using templates, but I have some problems with one of the overloads, I would like to make something like that: intvariable = fooclass; here's a pseudo...
30
by: Angel Tsankov | last post by:
Hello! Does the C++ standard define what happens when the size argument of void* operator new(size_t size) cannot represent the total number of bytes to be allocated? For example: struct S {...
4
by: fabian.lim | last post by:
Hi All, Im a newbie to C++, I am trying to customize the vector template STL to a template Class. My code is shown below. Im confused about something and maybe somebody here might be able to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.