1 Daijin

Assignment Operator Could Not Be Generator Definition

The super keyword is used to access and call functions on an object's parent.

The and expressions are valid in any method definition in both classes and object literals.

Syntax

super([arguments]); // calls the parent constructor. super.functionOnParent([arguments]);

Description

When used in a constructor, the keyword appears alone and must be used before the keyword is used. The  keyword can also be used to call functions on a parent object.

Example

Using in classes

This code snippet is taken from the classes sample (live demo). Here is called to avoid duplicating the constructor parts' that are common between and .

class Rectangle { constructor(height, width) { this.name = 'Rectangle'; this.height = height; this.width = width; } sayName() { console.log('Hi, I am a ', this.name + '.'); } get area() { return this.height * this.width; } set area(value) { this.height = this.width = Math.sqrt(value); } } class Square extends Rectangle { constructor(length) { this.height; // ReferenceError, super needs to be called first! // Here, it calls the parent class' constructor with lengths // provided for the Rectangle's width and height super(length, length); // Note: In derived classes, super() must be called before you // can use 'this'. Leaving this out will cause a reference error. this.name = 'Square'; } }

Super-calling static methods

You are also able to call super on static methods.

class Rectangle{ constructor() {} static logNbSides() { return 'I have 4 sides'; } } class Square extends Rectangle { constructor() {} static logDescription() { return super.logNbSides() + ' which are all equal'; } } Square.logDescription(); // 'I have 4 sides which are all equal'

Deleting super properties will throw an error

You can not use the delete operator and or to delete a parent class' property, it will throw a .

class Base { constructor() {} foo() {} } class Derived extends Base { constructor() {} delete() { delete super.foo; // this is bad } } new Derived().delete(); // ReferenceError: invalid delete involving 'super'.

can not overwrite non-writable properties

When defining non-writable properties with e.g. , can not overwrite the value of the property.

class X { constructor() { Object.defineProperty(this, 'prop', { configurable: true, writable: false, value: 1 }); } f() { super.prop = 2; } } var x = new X(); x.f(); // TypeError: "prop" is read-only console.log(x.prop); // 1

Using in object literals

Super can also be used in the object initializer / literal notation. In this example, two objects define a method. In the second object, calls the first object's method. This works with the help of with which we are able to set the prototype of to , so that is able to find on .

var obj1 = { method1() { console.log('method 1'); } } var obj2 = { method2() { super.method1(); } } Object.setPrototypeOf(obj2, obj1); obj2.method2(); // logs "method 1"

Specifications

Browser compatibility

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic support42 Yes45 No Yes Yes
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidOpera AndroidiOS SafariSamsung Internet
Basic support4242 Yes45 Yes Yes ?
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung InternetNode.js
Basic supportFull support 42Full support YesFull support 45No support NoFull support YesFull support YesFull support 42Full support 42Full support YesFull support 45Full support YesFull support Yes ? Full support Yes

Legend

Full support
Full support
No support
No support
Compatibility unknown
Compatibility unknown

See also

Document Tags and Contributors

 Contributors to this page:SphinxKnight, isomorphismes, LittleWhole, fscholz, koroliov, chrisdavidmills, kamalx, Soupedenuit, jameshkramer, joneit, nmve, spacegoing, kdex, Natalya_Surikova, jsx, senocular, arai, jpmedley, ErikL, Jeremie, kmaglione

 Last updated by:SphinxKnight,

The latest version of this topic can be found at Compiler Warning (level 4) C4512.

class' : assignment operator could not be generated

The compiler cannot generate an assignment operator for the given class. No assignment operator was created.

An assignment operator for the base class that is not accessible by the derived class can cause this warning.

To avoid this warning, specify a user-defined assignment operator for the class.

The compiler will also generate an assignment operator function for a class that does not define one. This assignment operator is a memberwise copy of the data members of an object. Because data items cannot be modified after initialization, if the class contains a item, the default assignment operator would not work. Another cause of the C4512 warning is a declaration of a nonstatic data member of reference type. If the intent is to create a non-copyable type, you must also prevent the creation of a default copy constructor.

You can resolve the C4512 warning for your code in one of three ways:

  • Explicitly define an assignment operator for the class.

  • Remove const or the reference operator from the data item in the class.

  • Use the #pragma warning statement to suppress the warning.

The following sample generates C4512.

// C4512.cpp // compile with: /EHsc /W4 // processor: x86 class Base { private: const int a; public: Base(int val = 0) : a(val) {} int get_a() { return a; } }; // C4512 warning class Base2 { private: const int a; public: Base2(int val = 0) : a(val) {} Base2 & operator=( const Base2 & ) { return *this; } int get_a() { return a; } }; // Type designer intends this to be non-copyable because it has a // reference member class Base3 { private: char& cr; public: Base3(char& r) : cr(r) {} // Uncomment the following line to explicitly disable copying: // Base3(const Base3&) = delete; }; // C4512 warning int main() { Base first; Base second; // OK Base2 first2; Base2 second2; char c = 'x'; Base3 first3(c); Base3 second3 = first3; // C2280 if no default copy ctor }

Leave a Comment

(0 Comments)

Your email address will not be published. Required fields are marked *