Skip to main content
GameDev.net gamedev.net
🔒 Locked

Value array of parent type with initializer list containing child type constructor

Started by Violet CLM Nov 17, 2021 at 4:58 AM 2 replies 5.7k views
Original Post
Violet CLM
Violet CLM

I don't really know what's going on here, and I doubt any of this code should be written in the first place, but the inconsistency that's occasionally printing B seems wrong. Or maybe it's intentional. I'm honestly not sure at this point.

class ReportA {
    void Report() const { print("A"); }
}
class ReportB : ReportA {
    void Report() const override { print("B"); }
}

void main() {
    array<ReportA> startWithConstructorA = {ReportA()};
    startWithConstructorA[0].Report(); //A
    startWithConstructorA = {ReportB()};
    startWithConstructorA[0].Report(); //A
    startWithConstructorA.insertAt(0, ReportB());
    startWithConstructorA[0].Report(); //A
	
    array<ReportA> startWithConstructorB = {ReportB()};
    startWithConstructorB[0].Report(); //B
    startWithConstructorB = {ReportB()};
    startWithConstructorB[0].Report(); //B
    startWithConstructorB.insertAt(0, ReportB());
    startWithConstructorB[0].Report(); //A
	
	ReportB variableB;
	array<ReportA> startWithVariableB = {variableB};
    startWithVariableB[0].Report(); //A
    startWithVariableB = {variableB};
    startWithVariableB[0].Report(); //A
    startWithVariableB = {ReportB()};
    startWithVariableB[0].Report(); //A
    startWithVariableB.insertAt(0, variableB);
    startWithVariableB[0].Report(); //A
    startWithVariableB.insertAt(0, ReportB());
    startWithVariableB[0].Report(); //A
}

As well as:

    array<ReportA> insert0 = {ReportB()};
	insert0.insertAt(0, ReportB());
	insert0 = {variableB}; //fine

	array<ReportA> insert1 = {ReportB()};
	insert1.insertAt(1, ReportB());
	insert1 = {variableB}; //An exception 'Mismatching types in value assignment' occurred.
WitchLord
WitchLord

I'll investigate this. It is clear that there are some inconsistent behavior here.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - <a href="http://www.angelcode.com/tower" rel

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.