Thread: overloading a new operator in a Class

  1. #1
    Unregistered
    Guest

    overloading a new operator in a Class

    Hello,
    Can I have a exmple of overloading a new operator in a Class.

    Thank You

  2. #2
    Unregistered
    Guest
    unless you have a very darned good reason, you should not overload the new operator. It is possible to do so, but should be done with significant hesitancy. The new operator will do it's routine job for (almost) all the classes you will want to use it with. If Cat is a user defined class:

    Cat * pCat = new Cat;
    Cat * aCat = new Cat[10];

    should work just fine. The new operator will use the default constructor so be sure the default constructor does what you want. If you use an array of user defined classes then only the default constructor can be used. You can use a non-default constructor if you are declaring just a single pointer to type class.

    Cat * pCat = new Cat(10);

    will use a non-default constructor that has as it's single non-default parameter an argument of type int.

  3. #3
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    I think he meant overloading a operator, not the new operator.??

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Inherite nonvirtual class functionality
    By DrSnuggles in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2009, 01:52 PM
  3. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  4. overloading the division operator with Integer Class
    By silk.odyssey in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2004, 06:59 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM