Operators that cannot be Overloaded - WHY?

A

Alex Vinokur

Victor Bazarov said:
Basically. It's more appropriate to say "using new operator" instead of
"calling" it.


We can.


Probably never.

V



Here are some samples of using overloaded and original "operator new".

Done under impression by
http://www.icce.rug.nl/documents/cplusplus/cplusplus09.html
(C++ Annotations Version 6.1.2 by Frank B. Brokken)



--------- C++ code : BEGIN ---------
// File foo.cpp
#include <iostream>
#include <cstring>
#include <cassert>
using namespace std;


struct Foo
{
int var1;
int var2;
void show ()
{
cout << "Values: var1 = " << var1 << ", var2 = " << var2 << endl;
}
Foo() {}
Foo (int var1_i, int var2_i) : var1 (var1_i), var2 (var2_i) {}
static void* operator new(size_t size_i);
static void* operator new(size_t size_i, int var1_i, int var2_i);
};

void* Foo::eek:perator new(size_t size_i)
{
cout << __PRETTY_FUNCTION__ << "; size = " << size_i << endl;
void *ptr = new char[size_i];
return ptr;
}

void* Foo::eek:perator new(size_t size_i, int var1_i, int var2_i)
{
cout << __PRETTY_FUNCTION__ << "; size = " << size_i << "; var1_i = " << var1_i << ", var2_i = " << var2_i << endl;
void *ptr = new char[size_i];
memcpy (ptr, (char*)&var1_i, sizeof var1_i);
memcpy ((char*)ptr + sizeof var1_i, (char*)&var2_i, sizeof var2_i);
return ptr;
}



int main()
{
Foo* foo1 = new Foo;
foo1->show();

cout << endl;
Foo* foo2 = new Foo();
foo2->show();

cout << endl;
Foo* foo3 = new Foo(100, 200);
foo3->show();

cout << endl;
Foo* foo4 = new(300, 400) Foo;
foo4->show();

cout << endl;
Foo* foo5 = new(500, 600) Foo ();
foo5->show();


cout << endl;
cout << endl;
Foo* foo6 = ::new Foo;
foo6->show();

cout << endl;
Foo* foo7 = ::new Foo();
foo7->show();

cout << endl;
Foo* foo8 = ::new Foo(700, 800);
foo8->show();

cout << endl;
Foo* foo9 = ::new Foo(900, 1000);
foo9->show();

// --------------------------------------------------------
// Using/calling 'new' as it shown below is not recommended
// --------------------------------------------------------

cout << endl;
cout << endl;
Foo* foo10 = (Foo*)Foo::eek:perator new(sizeof (Foo));
foo10->show();

cout << endl;
Foo* foo11 = (Foo*)Foo::eek:perator new(sizeof (Foo), 1100, 1200);
foo11->show();


return 0;
}

--------- C++ code : END -----------



--------- Compilation & Run : BEGIN ---------

$ g++ -v
[omitted]
gcc version 3.3.3 (cygwin special)


$ g++ foo.cpp
// No errors/warnings


$ a

static void* Foo::eek:perator new(unsigned int); size = 8
Values: var1 = 1628570148, var2 = 1628570148

static void* Foo::eek:perator new(unsigned int); size = 8
Values: var1 = 0, var2 = 0

static void* Foo::eek:perator new(unsigned int); size = 8
Values: var1 = 100, var2 = 200

static void* Foo::eek:perator new(unsigned int, int, int); size = 8; var1_i = 300, var2_i = 400
Values: var1 = 300, var2 = 400

static void* Foo::eek:perator new(unsigned int, int, int); size = 8; var1_i = 500, var2_i = 600
Values: var1 = 500, var2 = 600


Values: var1 = 0, var2 = 0

Values: var1 = 0, var2 = 0

Values: var1 = 700, var2 = 800

Values: var1 = 900, var2 = 1000


static void* Foo::eek:perator new(unsigned int); size = 8
Values: var1 = 0, var2 = 0

static void* Foo::eek:perator new(unsigned int, int, int); size = 8; var1_i = 1100, var2_i = 1200
Values: var1 = 1100, var2 = 1200

--------- Compilation & Run : END -----------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top