Advertisement

No constructor called on implicit construction with inout parameter reference

Started by May 04, 2024 11:09 AM
1 comment, last by WitchLord 6 months, 3 weeks ago

When passing an argument to function that accepts a &inout reference of type ‘Test’ which can be constructed implicitly with the argument's type, no constructor is called at all.

I believe this should behave either similar to TestB, where compilation fails. Or the proper constructor should be called.

This only affects &inout references. It works correctly for &in references or arguments passed by value.

class Test
{
    int32 Val;
    
    Test() { Val = 0; trace("ctor: " + Val); }
    Test(int32 x) { Val = x;  trace("ctor(int32): " + Val); }

    void Set(const Test& test) 
    {
        trace("Set");
        trace(" - Old Val: " + Val);
        trace(" - Parameter test.Val: " + test.Val);
        Val = test.Val;
        trace(" - New Val: " + Val);
    }
}

void Main()
{
    {
        trace("TestA");
        Test test;
        int32 val = 1;
        test.Set(val); // int32 constructor (implicit conversion from int32 to Test) of Test is not called
    }
    {
        // trace("TestB");
        // Test test;
        // test.Set(1); // Does not compile: "Error: Not a valid reference"
    }
    {
        trace("TestC");
        Test test;
        test.Set(Test(1)); // Constructing Test explicitly using int32 constructor
    }
    {
        trace("TestD");
        Test test;
        test.Set(cast<Test>(1)); // Explicitly casting from int32 to Test. Identical to TestC
    }
}

Output:

TestA
ctor: 0
Set
- Old Val: 0
- Parameter test.Val: 449
- New Val: 449
TestC
ctor: 0
ctor(int32): 1
Set
- Old Val: 0
- Parameter test.Val: 1
- New Val: 1
TestD
ctor: 0
ctor(int32): 1
Set
- Old Val: 0
- Parameter test.Val: 1
- New Val: 1

Thanks for letting me know of these issues.

There are definitely some incorrect behavior seen here. I'll analyse and have this fixed as soon as possible.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement