Re: Overloading "new" and "delete" operator

Discussion in 'C++' started by Min, Jun 27, 2003.

  1. Min

    Min Guest

    Finally, it works now, Thanks.
    I was at the local and university librarys searching for overloading
    operator new. All books I searched for talk only about overloading other
    operators such as >>, <<. ==, = etc. but not "new" "new []", "delete",
    "delete []"; I can't remember each titles, but I am sure none talks about
    the detail I was looking for. Even Effective C++ by Scott Meyers, the guru
    won't talk about "new [ ]" operator (only new)

    PS : Finishing "Writing Solid Code" by Steve Maguire (in C), I thought I
    should try some of the interesting techniques in my projects which are
    mostly in C++. Thats why I needed some help.

    Thanks again.

    "Victor Bazarov" <> wrote in message
    news:tlOKa.28172$3d.16772@sccrnsc02...
    > "Min" <> wrote...
    > > I am really stuck and can't find any good reference,

    >
    > What book do you have? I'd like to know to recommend others
    > not to get it since it's apparently of little help to those
    > who need help.
    >
    > > I hope some one here
    > > can help me.

    >
    > Someone sure can.
    >
    > > What I am trying to accomplish is writing my own "new" and "delete"

    > operator
    > > to keep a memory log in debug version for memory leak verification

    > purpose.
    > >
    > > Example:
    > > int * pInt = new int; // will call default

    > "new"
    > > Foo* pFoo = new Foo(); // will call the overloaded

    new.
    > > Foo* pFooArry = new Foo[100]; // will call the overloaded new.
    > >
    > > Also, help me clarify the difference between "Overloading Operator New"

    > and
    > > "Overloading New Operator".

    >
    > The function you overload (as you already have done) is called
    > "operator new". So, when you define your own in your class, you
    > "overload operator new". "overloading new operator" is something
    > I've not heard.
    >
    > > There is my attempt, but does not work, as someone has already pointed

    out
    > > that I also need to overload "[ ]" operator. But I don't get it,

    because
    > > all tutorials on overloading "[ ]" talk about accessing a data within

    the
    > > object, rather than an array of object.

    >
    > "someone" has pointed out that you need to overload operator new[],
    > not operator[].
    >
    > > Also, how would it work if we are
    > > also talking about inheritance.
    > > Thanks
    > >
    > > //***************************** CODE *************************
    > > class Foo
    > > {
    > > public:
    > > char name[20];
    > > Foo() { /*** Do Something ***/ }
    > > Foo( char* str) { /*** Do Something ***/ }
    > >
    > > static void * operator new(unsigned int size)

    >
    > Drop "static" here. It will be static because the language
    > dictates it, you don't have to declare it 'static'.
    >
    > > {
    > > //*********************
    > > // Code from Effective C++
    > > //*********************
    > > void* memory = null;
    > > if ( size == 0 ) { size = 1; }
    > > try
    > > {
    > > memory = ::eek:perator new(size);
    > > }
    > > catch (std::bad_alloc&)
    > > {
    > > throw;
    > > }
    > > return memory;
    > > }

    >
    > So, you need to add
    >
    > void * operator new[](size_t)
    > {
    > ...
    > }
    >
    > And you probably also want to add
    >
    > void operator delete(void *);
    > void operator delete[](void *);
    >
    > > };
    > >
    > > void main()

    >
    > 'main' returns 'int'.
    >
    > > {
    > > Foo * ptrFoo1 = new Foo(); // Uses Overloaded

    New
    > > Foo * ptrFoo2 = new Foo("What's up"); // Uses Overloaded New
    > > Foo *ptrFoo3 = new Foo[10]; // Uses DEFAULT New.
    > > }
    > > //***************** END OF CODE *************************
    > >
    > >

    >
    >
    Min, Jun 27, 2003
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. HeroOfSpielburg
    Replies:
    1
    Views:
    368
    Alf P. Steinbach
    Aug 6, 2003
  2. cppsks
    Replies:
    2
    Views:
    1,264
    Markus Elfring
    Oct 27, 2004
  3. cppsks
    Replies:
    0
    Views:
    402
    cppsks
    Oct 27, 2004
  4. cppsks
    Replies:
    0
    Views:
    798
    cppsks
    Oct 27, 2004
  5. Jef Driesen
    Replies:
    1
    Views:
    484
    Gernot Frisch
    Jan 19, 2005
Loading...

Share This Page