More Effective C ++ [3] clause is best not to deal with an array of multi-state mode

1. In the case of an array of polymorphic mix, access array elements will not be unexpected results (because access array elements will be used to index calculation)

The object is a subclass of the array is passed to a function declaration of the parent class object array, the compiler will allow this behavior, but because of the different memory structure subclass object and the parent object, may lead to abnormal operating results, since in this case the compiler is still assumed that the size of each element is the size of the parent class object element, but in fact at this time the size of each element is the size of a subclass object element

#include<bits/stdc++.h>
using namespace std;

class BST
{
public:
    char c;
};

class balancedBST: public the BST // public inheritance 
{
 public :
     int X;
};

void printBSTArray (S the ostream &, const the BST Array [], int numElements) // print base class array, the array type as a base note class! 
{
     For ( int I = 0 ; I <numElements; I ++ )
        s<<array[i].c;
    s<<endl;
}

int main ()
{
    BST array[3];
    for(int i=0;i<3;i++)
        array[i].c='A'+i;

    printBSTArray (COUT, Array, . 3 ); // transfer function into the array is the base class, the normal print 

    balancedBST barray [ . 3 ];
     for ( int I = 0 ; I < . 3 ; I ++ )
    {
        barray[i].x=i+1;
        barray[i].c='a'+i;
    }

    printBSTArray (COUT, barray, . 3 ); // transfer function into the array is a subclass of the printing anomalies! 
    / *
    Unusual reason:
    Because the array when the function definition of the object is defined for the base class, the compiler in subsequent operations,
    No matter passed into the function of the base class is a subclass of an array of objects or an array of objects, it will be treated as an array of objects base class
    And the memory configuration and memory configuration object subclass of the base class are not the same
    In this way, access to the index by something, is not something you want to visit

    array[i] *(array+i)
    between the spaced array [0] and array [i] i * sizeof (BST)

    Function in accordance with the i * sizeof (BST) count, and if you pass an array of base class objects, according to this calculation, it is certainly not something you want to access the
    */


}

/*
operation result:
ABC
ai
*/

2. In the case of polymorphic array of mix, remove array elements is also problematic, not defined will be used to operate (to remove the array of objects through a base class subclass pointer is undefined behavior in C ++!)

#include<bits/stdc++.h>
using namespace std;

class BST
{
public:
    char c;
};

class balancedBST: public the BST // public inheritance 
{
 public :
     int X;
};

void deleteArray(ostream& logStream,BST array[])
{
    LogStream << " the Deleting Array Adress AT: " << static_cast < void *> (Array) << endl;
     Delete [] Array; // underlying the operation of the index actually used, if the incoming array of objects is a subclass then to delete an array of objects by subclass the base class pointer is undefined behavior in C ++! 
}

int main ()
{
    BST *array =new BST[3];
    balancedBST * taught = new balancedBST [ 3 ];

    DeleteArray (COUT, Array); // passed is the base class object 

    DeleteArray (COUT, barray); // passed a subclass object, there is a problem underlying the use of undefined operations 
}


Summary: So how do you avoid these two problems?

The first approach is to avoid multi-state mode to process the Array

The second method is to avoid altogether concrete class inherit from another concrete class



Guess you like

Origin www.cnblogs.com/yinbiao/p/11747782.html