使用动态内存分配和重载赋值运算符

时间:2018-03-20 23:47:13

标签: c++ operator-overloading dynamic-memory-allocation

我正在学习C ++中的动态内存分配和重载操作符。 我正在尝试一个简单的程序来测试我的知识,但我无法找出我做错了什么。 这是代码:

{% macro defaultArea(context, name) %}
  {{ apos.area(context, name, {
    'apostrophe-rich-text': {},
    'apostrophe-images': {}
  }) }}
{% endmacro %}

我正在尝试使用operator =所以我可以将ptr的值从对象p赋给对象m,但是我得不到输出。我有什么想法吗?

2 个答案:

答案 0 :(得分:0)

Dockerfile

您只需访问char数组的一个元素。

我的更正:

FROM maven:3.5.3-jdk-8
ENV APP_DIR=/app
RUN mkdir -p $APP_DIR
WORKDIR $APP_DIR
COPY pom.xml .
RUN mvn -B dependency:resolve -Dclassifier=test
COPY . $APP_DIR
RUN mvn -B test-compile compile
ENTRYPOINT ["mvn", "test"]

答案 1 :(得分:0)

进行动态内存分配的最佳方法是让编译器为您执行此操作,可能与std::stringstd::vector等标准库容器配合使用。标准容器管理动态内存分配,无需担心抬起手指。

Read all about it.

如果你真的需要自己动手,请阅读Rules of zero, three, and five.

此外,了解copy-and-swap pattern. operator=的最佳方法是使用该模式。

这就是它的完成方式。当我忘记时,我保留了这个代码。正如您所看到的,正确地执行容器并非易事。这就是我们使用STL容器的原因。

namespace dj::example {

    template<class T>
    class my_vec
    {
    public:

        // constructors
        my_vec()
           : mSize(0)
           , mArray(nullptr)
        {}

        explicit 
        my_vec(std::size_t size) 
            : mSize(size)
            , mArray(new T[mSize]) // Can throw when size is huge
        {}

        // copy-constructor
        my_vec(const my_vec& other)
            : mSize(other.mSize)
            , mArray(new T[mSize]) // Can throw when size is huge
        {
            // For Visual C++
            // Put _SCL_SECURE_NO_WARNINGS in vc++ preprocessor 
            // to shut up VC++ about "deprecated" std::copy
            std::copy(other.mArray, other.mArray + mSize, mArray);
        }

        // destructor
        virtual ~my_vec()  // Make virtual in case my_vec is used as base class.
        {
            delete [] mArray;  
        }

        // Specialize external swap! Necessary for assignment operator below,
        // and for ADL (argument-dependant lookup).
        friend void swap(my_vec& first, my_vec& second) noexcept
        {
            // enable ADL (best practice)
            using std::swap; 

            // by swapping all the members of two objects,
            // they are effectively swapped
            swap(first.mSize, second.mSize); 
            swap(first.mArray, second.mArray);
        }

        // Assignment operator. Note that the argument "other" is passed by value.
        // This is the copy-and-swap pattern (best practice).
        // It assures that noexcept is true.
        my_vec& operator=(my_vec other) noexcept
        {
            swap(*this, other); // Uses specialized swap above. It must.
            return *this;
        } 

        // move constructor - construct and swap idiom (best practice)
        my_vec(my_vec&& other) noexcept
            : my_vec()
        {
            swap(*this, other);
        }


    // xxxxxxxxxxx
    // Everything below here is only to make my_vec a working example.
        // Member functions and typedefs below are for testing the example
        // ... not part of schema.

        using const_iterator = const T*;
        using iterator = T*;
        using size_type = std::size_t;
        using value_type = T;
        using type = my_vec<T>;

        iterator begin() { return mArray; }
        iterator end() { return mArray + mSize; }
        const_iterator begin() const { return mArray; }
        const_iterator end()  const { return mArray + mSize; }
        const_iterator cbegin() const { return mArray; }
        const_iterator cend()  const { return mArray + mSize; }
        T operator[](int i) const {
            return *(begin()+i);
        }
        T& operator[](int i) {
            return *(begin()+i);
        }
        std::size_t size()  const { return mSize; }

    private:
        std::size_t mSize;
        T* mArray;
    };

} // namespace dj