0

Are there any issues, like compile time operator, with sizeof associated with new?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vikas
  • 179
  • 1
  • 8

2 Answers2

5

new is an operator so you can overload it for some (or all) of your classes.

EDIT: I'll try to clarify to address @sbi's comments below.

Consider two classes:

class Base
{
}

class Derived : public Base
{
}

Let's assume that new() is a function, and we want to specialize it for the Base class and its descendants. We would have the "basic" new() function:

void *new(size_t size);

Overloaded functions cannot differ by their return types alone, so we can't do:

Base *new(size_t size);  // Illegal.

We can't use member functions because they need an existing instance to be called. We could use static member functions:

class Base
{
    static Base *new();
}

But the resulting syntax when allocating a instance of Derived would be quite awkward:

Derived *derived = Base::new();  // Uh?
Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
  • 1
    new operator can't be overloaded! It is the operator new that you can overload!! – Vikas Dec 20 '10 at 11:06
  • 1
    @Vikas, I'm afraid I don't understand what you're meaning here :) – Frédéric Hamidi Dec 20 '10 at 11:09
  • 2
    @Hamidi: new operator does following: 1. It calls operator new which returns void* 2. it typecasts returned pointer to appropriate type; 3. It calls constructor (if necessary). You can overload operator new NOT new operator because you cannot call constructor explicitly. – Vikas Dec 20 '10 at 11:11
  • @upvoter: whoever upvotes Hamidi's reply doesn't know the difference between new operator and operator new! – Vikas Dec 20 '10 at 11:14
  • See this first: http://stackoverflow.com/questions/1885849/difference-between-new-operator-and-operator-new – Vikas Dec 20 '10 at 11:16
  • @Vikas, if I understand you correctly, you're talking about the difference between an expression containing the `new` operator and `operator new()` itself. If your question refers to the former, I think that's a syntax issue: the array allocation form of `new` uses an array type specifier (e.g. `new Foo[12]`), and that syntax wouldn't be usable if it was a function. – Frédéric Hamidi Dec 20 '10 at 11:20
  • @Hamidi: There is another new operator "new []" for that! – Vikas Dec 20 '10 at 11:24
  • 1
    @Vikas upvoting something because you share the commenter's confusion is a perfectly good use of SO, and complaining about it is a bit annoying - what you should do (as you've done) is just explain it better. +1 for your explanation BTW. :) – Karl Knechtel Dec 20 '10 at 11:31
  • Guys, why new is an operator not a function? – Vikas Dec 20 '10 at 11:37
  • The `new` operator calls `operator new`, as you pointed out. It calls `operator new` instead of a function, because if it called a function then you would have to be able to define that function, which would mean making a function named `new`, but you can't do that because `new` is a keyword. Or else they **could** specify some "magic" name of a function to call, but that is exactly what the `operator` keyword is for - to create otherwise-impossible "function names". So basically, the answer is "the same reason that you override `operator +` instead of `__add__` (i.e. Python-style)". – Karl Knechtel Dec 20 '10 at 11:48
  • @Vikas: Are you, by chance, referring to [new expression vs. operator new](http://stackoverflow.com/questions/1820069/public-operator-new-private-operator-delete-getting-c2248-can-not-access-priva/1820092#1820092) rather than new operator vs. operator new? – sbi Dec 20 '10 at 11:53
  • @Frédéric: Functions can be overloaded, too, so this is a non-answer. – sbi Dec 20 '10 at 11:56
  • @sbi: there are a couple of places in the standard which use "new operator", apparently to mean a *new-expression*. Surprised me, too. – Steve Jessop Dec 20 '10 at 12:18
  • @sbi, overloaded functions cannot differ by return type alone, so how would you implement `operator new()` that way? Were you thinking of template specialization? – Frédéric Hamidi Dec 20 '10 at 12:35
  • @Frédéric: `operator new` always return `void*`. – sbi Dec 20 '10 at 13:13
  • @sbi, yes, that was my point :) `operator new` can be overloaded for different classes because each class can provide its own implementation (that returns `void *`). It's not possible to do the same with overloaded functions because they would either all return `void *` or only differ by their return value, which is not supported. Using member functions is also out of the question, since you would need an existing instance of the class in order to allocate a new instance of the class. – Frédéric Hamidi Dec 20 '10 at 13:18
  • @Frédéric: And why wouldn't a new expression be able to call `malloc()` instead of `operator new` for allocating memory? – sbi Dec 20 '10 at 14:47
3

I think the short answer to your question is that the new operator isn't a function, because it is the new that appears in a new-expression, and new-expression doesn't follow the function call syntax. How can it be a function, when one of the possible components/operand of a new-expression is a type-id? Could you define the syntax of a new-expression such that new would be a function, presumably overloaded on the type being allocated? And if you could, would Bjarne Stroustrup have preferred that syntax to the syntax he actually invented, and that ISO standardized?

To be honest, I think that in everyday English the standard is pushing its luck classifying the new-expression as a "unary expression" at all. If it is, then its "single" operand consists of up to three separate parts (new-placement, new-type-id, new-initializer). Not that it really matters - it is what it is, and AFAIK the standard doesn't define any properties which apply to all operators, so it doesn't matter that much whether you call it an operator, or just a keyword used to introduce a new-expression.

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699