I just found this out when redeclaring data members in a extended type. It's not a tutorial really. But it's good knowledge to have about extending types in BlitzMax.
Given this code:
Results in:
But when reading the code you expect it to be this:
Why does this happen? It seems that when you 'redeclare' a data member that already exists in the base type it becomes a data member that doesn't exist in the base type.
This can lead to some confusing situations.
For example, if in the base type you use the New() method to read the value of data member 'a' to do some calculations or something, it uses the default value of the base type. Which may result in unexpected results.
Even if you don't redeclare the data member, when overloading New() it first executes the New() method of the base type, and then the New() method of the extended type.
I believe in C++ this is done in reversed order. Which is more logical to me.
If you redeclare a data member that has been declared in the base type then when it's casted back to the base type, the value of the redeclared data member will be that of the base type.
If you haven't redeclared the data member and you cast it back to the base type then it'll contain the value of the extended type.
------------------------
Now, if I can open up a little discussion about this subject: Do you think this behavior is wanted? The 'issue' with redeclared data members is something I can understand. But what about the order of how the New() method is being executed? I'm almost sure that with C++ it's reversed. Although of course, I could be wrong.
Given this code:
SuperStrict Type TBase Field a:Int End Type Type TExtended Extends TBase Field a:Int = 10 End Type Local obj:TExtended = New TExtended Print "TExtended.a = " + obj.a Print "Casted to TBase makes a = " + TBase(obj).a
Results in:
TExtended.a = 10 Casted to TBase makes a = 0
But when reading the code you expect it to be this:
TExtended.a = 10 Casted to TBase makes a = 10
Why does this happen? It seems that when you 'redeclare' a data member that already exists in the base type it becomes a data member that doesn't exist in the base type.
This can lead to some confusing situations.
For example, if in the base type you use the New() method to read the value of data member 'a' to do some calculations or something, it uses the default value of the base type. Which may result in unexpected results.
Even if you don't redeclare the data member, when overloading New() it first executes the New() method of the base type, and then the New() method of the extended type.
I believe in C++ this is done in reversed order. Which is more logical to me.
If you redeclare a data member that has been declared in the base type then when it's casted back to the base type, the value of the redeclared data member will be that of the base type.
If you haven't redeclared the data member and you cast it back to the base type then it'll contain the value of the extended type.
------------------------
Now, if I can open up a little discussion about this subject: Do you think this behavior is wanted? The 'issue' with redeclared data members is something I can understand. But what about the order of how the New() method is being executed? I'm almost sure that with C++ it's reversed. Although of course, I could be wrong.