国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

About the destructor of C++ class template
漂亮男人
漂亮男人 2017-05-16 13:25:14
0
1
809

1. I just learned C++ in my freshman year and encountered some problems. If there is something wrong, I hope you can give me some advice.
2. When instantiating a member function of a class template, why is the destructor automatically called after the function ends? For example

~set()
{
    cout<<"destory!"<<endl;
}
private:
    int size;
    T *p;
void set<T>::intersection(set s2) //交集
int cnt = 0;
int size2 = s2.size;
sort(p, p + size);
sort(s2.p, s2.p + size2);
int MaxSize = max(size, size2);
T *tmp = new T[MaxSize];

if (MaxSize == size)//集合A含元素個數(shù)大于B
    for (int i = 0; i<MaxSize; i++)
        if (s2.b_search(p[i])) //二分查找
            tmp[cnt++] = p[i];

else if (size != size2)
    for (int i = 0; i<MaxSize; i++)
        if (b_search(s2.p[i]))
            tmp[cnt++] = s2.p[i];

for (int i = 0; i<cnt - 1; i++)
    cout << tmp[i] << ",";
cout << tmp[cnt - 1];

In this case
int a2[] = { 2,4,1,6,0 };
int a4[] = { 2,4,6,8 ,9 };
set<int> t2(a2, 5);
set<int> t4(a4, 5);
When calling t2.intersection(t4);, destory! will be displayed at the end. Why is the destructor called so early? At this time, if delete []p is added to the destructor, an error will be reported@_@

漂亮男人
漂亮男人

reply all(1)
伊謝爾倫
//這里的s2會進行拷貝構(gòu)造,變成 (set &s2) 就沒有事了
void set<T>::intersection(set s2){
//start
//...
//end
} // 這里的s2會調(diào)用析構(gòu)


Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template