或者,是否还有其他已知的雇用__(创新)的负面影响? 我看不到任何问题。
I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...
或者,是否还有其他已知的雇用__(创新)的负面影响? 我看不到任何问题。
MSCV uses one vptr per object and one vtbl per class
to implement OO mechanism such as RTTI and virtual functions.
So RTTI and virtual functions will work fine if and only if the vptr has been set correctly.
struct __declspec(novtable) B {
virtual void f() = 0;
};
struct D1 : B {
D1() {
} // after the construction of D1, vptr will be set to vtbl of D1.
};
D1 d1; // after d has been fully constructed, vptr is correct.
B& b = d1; // so virtual functions and RTTI will work.
b.f(); // calls D1::f();
assert( dynamic_cast<D1*>(&b) );
assert( typeid(b) == typeid(D1) );
B should be an abstract class when use __declspec(novtable)
.
There will be no instance of B except in the constructor of D1.
And __declspec(novtable) has no negative affects in most case.
但是,在建立衍生类别时, declspec(novtable)
将使之不同于ISO C++的语义。
struct D2 : B {
D2() { // when enter the constructor of D2
// the vtpr must be set to vptr of B
// if B didn t use __declspec(novtable).
// virtual functions and RTTI will also work.
this->f(); // should calls B::f();
assert( typeid(*this) == typeid(B) );
assert( !dynamic_cast<D2*>(this) );
assert( dynamic_cast<B*>(this) );
// but __declspec(novtable) will stop the compiler
// from generating code to initialize the vptr.
// so the code above will crash because of uninitialized vptr.
}
};
Note: virtual f() = 0
; makes f to be a pure virtual function
and B to be an abstract class.
The definition
of a pure virtual function could
(not must
) be missing.
C++ allows virtual function call in constructor which we don t recommend.
Update: A mistake in D2 : the vptr in derived constructor.
struct D3 : B { // ISO C++ semantic
D3() { // vptr must be set to vtbl of B before enter
} // vptr must be set to vtbl of D2 after leave
};
但是,在施工期间,变压器并不确定。 虚拟功能在构造中起不建议的原因之一。
If vptr in D2::D2() was B and definition of B::f() was missing, this->f();
will crash when dereference pointer-to-function in the vtbl.
If vptr in D2::D2() was B and B use novtable, this->f();
will crash when dereference an uninitialized vptr.
In fact , vptr in D2::D2() is D2 in MSVC(msvc8).The compiler set vptr to D2 before execute other code in D2::D2().
So this->f();
calls D2::f() and the three assertions will be violated.
if I understand it correctly: Any virtual fn call inside a ctor or a dtor is converted to a compile time linking. We cannot make virtual fn calls from the (c/d)tors. The reason is that at the time the object of the base class is getting created it has no knowledge of the derived class and hence cannot make the call to derived class and w.r.t the dtors the same logic applies.
I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...
I have been searching for sample code creating iterator for my own container, but I haven t really found a good example. I know this been asked before (Creating my own Iterators) but didn t see any ...
Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...
I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?
Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...
Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...
I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...
Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?