Menu

keywords-new

Will Pittenger

Overview

The new keyword allocates memory when used in code and calls the constructor for the type the memory is being allocated for. When used as part of the declaration of a data member, method, property, nested type; it allows you to override something in your base type that would be hidden by your new member, method, property, or nested type.

When declaring member, method, property, or nested type

Normally, ASIL compilers must issue an error if you're declaring something that would hide something inside your base type. (If the method or property is virtual, use override instead.) This changes if you add the new keyword to the declaration. Now your data member, method, property, or nested type is visible instead of the one in the base type. The code sample below shows only part of the declaration.

Note: Since namespaces in ASIL can derive from one another, you might need new to hide a member from your base-namespace.

Procedures

:::text
/AccessQualifiersAndOtherModifiers/ [\new\] (\command\ | \function\ | \statement\) /identifier/
  ' Implement it

/AccessQualifiersAndOtherModifiers/ varies depending on what you're declaring. Everything needs an access qualifier. The keywords const, static, final, virtual, and abstract might or might not be valid.

Types

:::text
/AccessQualifiersAndOtherModifiers/ [\new\] (\enum\ [\struct\] | [\struct\] | [\class\]) /identifier/
  ' Declare the rest of the type

/AccessQualifiersAndOtherModifiers/ varies depending on what you're declaring. Everything needs an access qualifier unless its global and outside a space. The inner keyword can be applied if the type is nested inside another type and not just a namespace. Any type may or may not need const, final, abstract, or static.

Constants

:::text
(\public\ | \protected\ | \private\) [\new\] \const\ /TypeDescriptor/ /identifier/ \=\ /DefaultValue/

For what /TypeDescriptor/ can be, see Type descriptors.

Other data members

This isn't required for local variables.

:::text
(\public\ | \protected\ | \private\) [\new\] \var\ /TypeDescriptor/ /identifier/ [\=\ /DefaultValue/

Properties

:::text
(\public\ | \protected\ | \private\) [(\virtual\ | \final\ | \abstract\)] [\static\] [\new\] \property\ /TypeDescriptor/ /identifier/
  [[(\virtual\ | \final\ | \abstract\)] \get\
    ' return some value
  ]

  [[(\virtual\ | \final\ | \abstract\)] \set\
    ' Do something with the value keyword
  ]

When allocating memory

When you allocate memory with the new keyword, it calls the creator for the type followed by the constructor for the new instance. It then returns a reference to the new instance. Any parameters you pass to new are also passed to the creator and constructor.

:::text
`` Do something with the instance `` \=\ \new\ /ParameterList/

You can also place everything from the new keyword through the last parameter to be passed to the creator and constructor, but no more, in parentheses. This lets you access members with the . operator.

:::text
(new /ParameterList/)\.\/SomeMember/

Related

Wiki: Constructors, creators, destructors, and garbage collection
Wiki: Keywords
Wiki: Reflection
Wiki: Type casts
Wiki: keywords-abstract
Wiki: keywords-const
Wiki: keywords-constructor
Wiki: keywords-creator
Wiki: keywords-final
Wiki: keywords-inner
Wiki: keywords-override
Wiki: keywords-throw
Wiki: operators-braces
Wiki: operators-brackets