Thread: interesting question about new and nothrow new

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    interesting question about new and nothrow new

    Hello everyone,


    Why "if classes provide their own new but forget to provide nothrow new too, nothrow new will be hidden and won't even work."

    http://www.gotw.ca/publications/mill16.htm

    BTW: I understand we should avoid nothrow new, and now I am just interested to know why if only provide normal new, there will be issues?


    thanks in advance,
    George

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    That's just part of normal overloading rules. If the compiler finds a function of the right name in one scope, it won't bother looking beyond that, even if none of the overloads in the given scope actually fit.

    It's exactly the same as hiding functions in a base class.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks CornedBee,


    I have read through the article, and I think if we only provide one version of new in current class, compiler will stop in current class searching for the other 2 forms of overloaded new even if current class does not provide it (they are defined in base class or global space).

    The issue of providing only one form of new is, when other class uses the other two forms of new, since in current class, it is not implemented, there will be compile error.

    My understanding correct?

    BTW: from the article, I am not sure why it is a compile time error, it should be more like a runtime error?


    regards,
    George

    Quote Originally Posted by CornedBee View Post
    That's just part of normal overloading rules. If the compiler finds a function of the right name in one scope, it won't bother looking beyond that, even if none of the overloads in the given scope actually fit.

    It's exactly the same as hiding functions in a base class.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It's a compile time error, because the compiler sorts out which function is being called for "new" on a particular object, and which variation of "new" it is to call based on the arguments given.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Mats,


    Your reply is clear. My question is answered.

    Quote Originally Posted by matsp View Post
    It's a compile time error, because the compiler sorts out which function is being called for "new" on a particular object, and which variation of "new" it is to call based on the arguments given.

    --
    Mats

    regards,
    George

Popular pages Recent additions subscribe to a feed