🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

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

Started by
1 comment, last by WitchLord 2 years, 7 months ago

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.

Advertisement

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 - Tower - free puzzle game

This topic is closed to new replies.

Advertisement