简体   繁体   中英

Access to the template class constructor's parameter from the destructor, is it possible?

I want to see how is possible to access the constructor's parameter from the destructor. In this example I want to modify the parameter from the constructor and restore it in destructor.

template<typename T>
class sorted_array_view {
public:    
    sorted_array_view(T* givenArray,size_t size) {
         for( int idx = 0; idx < size; ++idx){
            data.push_back(givenArray[idx]);
            originaldata.push_back(givenArray[idx]);
         }
         std::sort(data.begin(), data.end());
         for(int idx =0; idx<size; ++idx){
            givenArray[idx] = data.at(idx);
         }
    }

    ~sorted_array_view(){
         for(int idx =0; idx<size; ++idx){
            givenArray[idx] = originaldata.at(idx);
         }
    }

private:
    std::vector<T> data;
    std::vector<T> originaldata;
};

But I got error 'givenArray' was not declared in this scope at destructor. How can I access to the givenArray ?

Just make givenArray a data member of the class.

Then the destructor (and any other member function) will have access to it.


PS: Change the size in the destructor to originaldata.size() , if their value is going to be same (which seems to be case from your code). Otherwise, you will need size to become a data member too.

In general, in a class, if you want to access an argument of a member function from another member function, you need to store that argument in the class.

Overall, simplifying your code, it gives:

template<typename T>
struct sorted_array_view
{    
    sorted_array_view(T* array, std::size_t size)
        : original_content{array, array+size}, original_container{array}
    {
        std::sort(array, array+size);
    }

    ~sorted_array_view()
    {
        std::copy(begin(original_content), end(original_content), original_container);
    }

private:
    std::vector<T> original_content;
    T*             original_container;
};

Full demo: http://coliru.stacked-crooked.com/a/2958a997404bfaf9

Using more STL, your code can even become

#include <iostream>
#include <vector>
#include <algorithm>
template<typename T>
class sorted_array_view {
public:
    sorted_array_view(T* const givenArray, size_t size)
        : data(givenArray, givenArray + size)
        , originaldata(givenArray, givenArray + size)
        , givenArray(givenArray)
    {
        std::sort(data.begin(), data.end());
        std::copy(std::cbegin(data), std::cend(data), givenArray);
    }

    ~sorted_array_view() {
        std::copy(std::cbegin(originaldata), std::cend(originaldata), givenArray);
    }

private:
    std::vector<T> data;
    std::vector<T> const originaldata;
    T* const givenArray;
};

#include <array>
#include <iostream>
int main()
{
    std::array<int, 5> givenArray = { 4, 3, 5, 1, 2 };

    {
        sorted_array_view<int> sortTemp(&givenArray[0], givenArray.size());

        for (const auto& el : givenArray) std::cout << el << " "; std::cout << "\n";
    }
    for (const auto& el : givenArray) std::cout << el << " "; std::cout << "\n";
}

Output:

1 2 3 4 5 
4 3 5 1 2 

live example

But it's dangerous to do too much in your constructor/destructor. You cannot throw an exception from a destructor. edit: see comment by Peter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM