I just noticed some weird behavior while playing with classes. Consider the following code
Code: Select all
class Base
{
public Base() { DebugN("Base"); i = 1; }
public string values() { return ("Base::i = " + i); }
protected int i;
};
class Derived : Base {
public Derived() { DebugN("Derived"); i = 2; j = 22; }
public string values() { return ("Derived::i = " + i + ", j = " + j); }
private int j;
};
main()
{
shared_ptr p = new Base();
DebugN(p.values());
DebugN("------------------------------------------------------");
p = nullptr;
p = new Derived();
DebugN(p.values());
}
The line p = nullptr; should not have any effect. Assigning an object to p the line after should completely remove any previous content that it pointed to.
Surprisingly the behavior changes depending on the existence of that line..
With the line included, it will output the following, expected, log output:
However, removing that line results in the following and rather unexpected log:WCCOAui1:["Base"]
WCCOAui1:["Base::i = 1"]
WCCOAui1:["------------------------------------------------------"]
WCCOAui1:["Base"]
WCCOAui1:["Derived"]
WCCOAui1:["Derived::i = 2, j = 22"]
As you can see it runs through the Derived constructor and assigns the value to i, but it calls the base values() method instead of the derived version.WCCOAui1:["Base"]
WCCOAui1:["Base::i = 1"]
WCCOAui1:["------------------------------------------------------"]
WCCOAui1:["Base"]
WCCOAui1:["Derived"]
WCCOAui1:["Base::i = 2"]
Best regrads
Frank