Hello,
Can I have a exmple of overloading a new operator in a Class.
Thank You
Hello,
Can I have a exmple of overloading a new operator in a Class.
Thank You
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.
I think he meant overloading a operator, not the new operator.??