• 二阶构造函数


    class IntArray
    {
    private:
    int m_length;
    int* m_pointer;

    IntArray(int len);
    IntArray(const IntArray& obj);
    bool construct();
    public:
    static IntArray* NewInstance(int length);
    int length();
    bool get(int index, int& value);
    bool set(int index ,int value);
    ~IntArray();
    };
    IntArray::IntArray(int len)
    {
    m_length = len;
    }

    bool IntArray::construct()
    {
    bool ret = true;

    m_pointer = new int[m_length];

    if( m_pointer )
    {
    for(int i=0; i<m_length; i++)
    {
    m_pointer[i] = 0;
    }
    }
    else
    {
    ret = false;
    }

    return ret;
    }

    IntArray* IntArray::NewInstance(int length)
    {
    IntArray* ret = new IntArray(length);

    if( !(ret && ret->construct()) )
    {
    delete ret;
    ret = 0;
    }

    return ret;
    }

    int IntArray::length()
    {
    return m_length;
    }

    bool IntArray::get(int index, int& value)
    {
    bool ret = (0 <= index) && (index < length());

    if( ret )
    {
    value = m_pointer[index];
    }

    return ret;
    }

    bool IntArray::set(int index, int value)
    {
    bool ret = (0 <= index) && (index < length());

    if( ret )
    {
    m_pointer[index] = value;
    }

    return ret;
    }

    IntArray::~IntArray()
    {
    delete[]m_pointer;
    }

  • 相关阅读:
    MongoDB的基本操作
    Python 进阶 之 协程
    sublime text3 3143 注册码
    git add 文档
    Corosync 配置描述
    Centos 7 设置 DNS
    2017百度春招<度度熊买帽子的问题>
    leetcode 160. Intersection of Two Linked Lists
    leetcode 155. Min Stack
    leetcode 141 142. Linked List Cycle
  • 原文地址:https://www.cnblogs.com/judes/p/8186419.html
Copyright © 2020-2023  润新知