JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.
If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.
If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.
| CodeSonar® 9.2p0 | CONFIDENTIAL | CodeSecure Inc |
A virtual member function is called from a constructor.
This can lead to unexpected and potentially dangerous behavior. During execution of a base class B's constructor, when not in an even deeper base class's constructor, any virtual calls through the this pointer will resolve as if B is the dynamic type (most derived type) of this.
For inline constructors, a warning will only be issued if the constructor is invoked at least once.
| クラス名 | Virtual Call in Constructor | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 日本語クラス名 | Virtual Call in Constructor | ||||||||||||||||||||||||
| クラス分類 | 信頼性 (reliability) | ||||||||||||||||||||||||
| ニーモニック | LANG.STRUCT.VCALL_IN_CTOR | ||||||||||||||||||||||||
| カテゴリー |
|
||||||||||||||||||||||||
| 対応言語 | C++ のみ利用可能です。 C は利用できません。 |
||||||||||||||||||||||||
| 有効/無効設定 | このワーニングクラスのチェックはデフォルトで有効になっています。チェックを無効にするにはプロジェクト設定ファイル
(configuration file)に以下の WARNING_FILTER ルールを追加してください。
WARNING_FILTER += discard class="Virtual Call in Constructor" |
namespace lang_struct_vcall_in_ctor {
// Code with explicit inline constructor invocation.
class BaseExplicit {
virtual void virtual_setup(void) {}
public:
BaseExplicit() {
this->virtual_setup(); // 'Virtual Call in Constructor' warning issued here.
// This call to virtual_setup() will always invoke BaseExplicit::virtual_setup(),
// which is probably not what the author intends.
}
};
class DerivedExplicit : BaseExplicit {
int i;
virtual void virtual_setup(void) override {i=5;}
};
BaseExplicit * use_base_explicit(void){
BaseExplicit *be = new BaseExplicit(); // Explicit invocation of inline constructor.
return be;
}
// Code with implicit inline constructor invocation.
class BaseImplicit {
virtual void virtual_setup(void) {}
public:
BaseImplicit() {
this->virtual_setup(); // Two 'Virtual Call in Constructor' warnings issued here
// - one for the virtual call in BaseImplicit::BaseImplicit()
// - one for the virtual call in DerivedImplicit::DerivedImplicit()
}
};
class DerivedImplicit : BaseImplicit {
int i;
virtual void virtual_setup(void) override {i=5;}
public:
int value(void){
return i; // 'Uninitialized Variable' warning issued here
// - when use_derived_implicit() calls d.value(), the issues with the virtual
// call in the constructor mean that d.i has NOT actually been initialized.
}
};
int use_derived_implicit(void){
DerivedImplicit d; // Implicit invocation of inline BaseImplicit constructor
// (and then DerivedImplicit constructor).
return d.value();
}
}
設定ファイルの以下のパラメータがこのワーニングクラスのチェックに影響します。
To report problems with this documentation, please visit https://support.codesecure.com/.