您认为以下两种情况下的性能/内存利用率会有什么不同吗?编译器是否会优化第一段代码以使两者之间没有任何区别。
class VersionBase
{
public:
VersionBase(int iVer) : m_iVersion(iVer) {…};
int GetVersion() const { return m_iVersion; };
private:
const int m_iVersion;
}
class SomeVersionedDataObject : VersionBase
{
VersionedDataObject() : VersionBase(2) {…}; // Set version to whatever is the latest for the class
…
…
}
对比
class SomeVersionedDataObject
{
public:
VersionedDataObject()
int GetVersion() const { return m_iVersion; };
private:
static const int m_iVersion = 2;
}