Although the C + + programming language is powerful and flexible in application, there are also various errors in the actual programming. Here we will give you a detailed description of the C + + pointer drift solution, I hope this article describes the content can help you solve the problem.
We have recently encountered a strange problem in our work, and we finally determined that the C + + pointer drift caused by multiple inheritance is related to the C + + object model. The signal is as follows:
Class A {...}; Class b{...}; Class Ab:public B, public A {...} ...} AB *pab = new AB (); A * PA = (A *) PAB; b* PB = (b*) PAB; This is when you find that the value of PA and PB is not the same! One of them is equal to the PAB and the other has an offset. If you change the order of A and B in AB's statement, the pointer that produces the offset becomes another.
To make sure that this is the compiler doing the conversion, use the void pointer to fool the compiler:
void *PV = (void*) PAB; PA = (A *) PV; The value of the PA is the same as the PAB, but it points to the wrong place. The conversion from PAB to PA relies on the choice of paths that are not very reassuring. Do not know to put the pointer into the container and then remove it, will not be wrong. Of course, the above uses mandatory type conversions, which should be avoided in good programs. If you have an implicit conversion, you can get the correct result:
Std::vector v; Implicit type conversion V.insert (V.begin (), PAB); void *PV = v[0]; PA = (A *) PV; The following programs are compiled by using cygwin/g++b:
#include #include Class A {public:int A;}; Class B {Public:int B;}; Class Ab:public B, public A {public:int AB; int main (int argc, char **argv) {AB *pab = new AB (); pab->ab = 1; pab->b = 2; pab->a = 3; A * PA = (A *) PAB; b* PB = (b*) PAB; printf ("AB:%pn" "A:%pn" "B:%pn", PAB, PA, Pb); Std::vector v; Implicit type conversion V.insert (V.begin (), PAB); void *PV = v[0]; PA = (A *) PV; printf ("PV is%pnpa is%pnpab%s PVN", PV, PA, (PAB = PV)? "==" : "!="); printf ("A.A is%dn", pa->a); Forced type Conversion PV = (void*) PAB; PA = (A *) PV; printf ("Now A.A is%dn", pa->a); Run Results:
ab:0x6b01f0 a:0x6b01f4 b:0x6b01f0 PV is 0x6b01f4 PA C + + programming language template application is a more complex technology, we begin today from the C + + KMP algorithm template Basic application began to learn, and deepen our knowledge of this The understanding degree, facilitates the future application, enhances the programming efficiency.
You can add these two lines of code when you're using it.
#include < vector> using namespace std;
C + + KMP algorithm template parameter description
Const T *source string to be matched
TL Sourcelen The length of the string to be matched
Const T *pattern Pattern string
TL Mode string length
C + + KMP algorithm template code example:
Template < class t,class tl> inline int kmpmatch (const t *SOURCE,TL sourcelen,const T *pattern,tl Patternlen)
{vector< int> next;
for (int i = 0; i < Patternlen i + +) next.push_back (0); Next[0] =-1; for (int i = 1; i < Patternlen i + +)
{int j = next[i-1];
while ((Pattern[i]!= pattern[i + 1]) && (J >= 0))
{j = next[j];}
if (pattern[i] = = pattern[j + 1])
{Next[i] = j + 1;}
else {Next[i] =-1;}}
int i = 0; int j = 0;
while ((I < Sourcelen) && (J < Patternlen))
{if (source[i] = = Pattern[j])
{i + +; j + +;} else if (j = = 0)
{i + +;} else {j = next[j-1] + 1;}}
if (J >= Patternlen)
{if (!next.empty ())
Next.clear ();
return I-patternlen;
}
Else
{if (!next.empty ()) next.clear ();
return-1; } }
Is 0x6b01f4 PAB!= PV a.a are 3 now A.A is 2