HaxeFoundation / HaxeFoundation/hxcpp

Calling a method on a null value class is allowed.

Open
#1,110 8 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
330
Forks
227
Avg merge
2d 16h
Merged PRs (30d)
18

Description

If you have an array of classes and you try and index a non-existent index. It will allow you to call methods just fine. But if said method tries to access a variable of the class it will crash, because `this` is null (however it can call other methods of itself)

This is very weird behaviour to me, but it might be intentional.

```hx
class CoolClass {
public var num:Int;

public function new(i:Int) {
num = i;
}

public function hi() {
trace(this);
trace(num);
}
}

class Main {
static function main() {
trace('Begin');
var myCoolClasses:Array = [];

myCoolClasses.push(new CoolClass(1));
myCoolClasses.push(new CoolClass(2));

// Index 0 and 1 available, lets call indexes 0, 1 and 2
myCoolClasses[0].hi();
myCoolClasses[1].hi();
myCoolClasses[2].hi();
trace("End");
}
}
```

![afbeelding](https://github.com/HaxeFoundation/hxcpp/assets/70071615/d8b2d0d1-ff7f-4782-b78f-8489cb20d059)

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.