如果是向量中的对象,是否会调用析构函数?

时间:2016-05-10 13:59:43

标签: c++ memory destructor

这是我课程的作业,我无法理解背后的原因。

我们有一个没有指针和类Car的类Person,如下所示:

%REM
    Library Class.JSON by Karl-Henry Martinsson
    Created Oct 9, 2014 - Initial version
    Updated Nov 6, 2015 - Added JSONP support
    Description: Class to generate simple JSON from values
%END REM

Option Public
Option Declare

Class JSONdata
    Private p_json List As String

    Public Sub New()
        '*** Set default value(s)
        me.p_json("ajaxstatus") = ""
    End Sub

    %REM
        Property Set success
        Description: Set success to true or false
    %END REM
    Public Property Set success As Boolean
        If me.success Then 
            Call me.SetValue("ajaxstatus","success")
        Else
            Call me.SetValue("ajaxstatus","error")
        End If
    End Property

    %REM
        Property Get success
        Description: Not really used...
    %END REM
    Public Property Get success As Boolean
        If me.p_json("ajaxstatus") = |"success"| Then
            me.success = True
        Else
            me.success = False
        End If
    End Property

    %REM
        Sub SetMsg
        Description: Set msg item
    %END REM
    Public Sub SetMsg(message As String)
        Call me.SetValue("msg",message)
    End Sub

    Public Sub SetErrorMsg(message As String)
        Call me.SetValue("errormsg",message)
        me.success = False
    End Sub

    Public Sub SetValue(itemname As String, value As String)
        Dim tmp As String
        Dim delimiter As String
        '*** Check for quote (double and single) and fix value if needed
        tmp = Replace(value,Chr$(13),"<br>")
        tmp = FullTrim(Replace(tmp,Chr$(10),""))
        If InStr(tmp,|"|)>0 Then
            If InStr(tmp,|'|)>0 Then
                tmp = Replace(tmp,|"|,|"|)
                delimiter = |"|
            Else
                delimiter = |'|
            End If
        Else
            delimiter = |"|
        End If
        '*** Store value with delimiter in list
        me.p_json(itemname) = delimiter & tmp & delimiter
    End Sub

    Public Sub SetData(itemname As String, value As String)
        '*** Store value in list
        me.p_json(itemname) = value
    End Sub

    %REM
        Function GetJSON
        Description: Return a JSON object as text
    %END REM
    Function GetJSON As String
        Dim json As String
        '*** Opening curly braces + CR
        json = "{" + Chr$(13)
        '*** Loop through all list elements and build JSON
        ForAll j In me.p_json
            json = json + |"| + ListTag(j) + |":| + j + "," + Chr$(13)
        End ForAll
        '*** Remove the comma after the last item
        json = Left$(json,Len(json)-2) + Chr$(13)
        '*** Add closing curly bracket and return JSON
        json = json + "}"
        GetJSON = json 
    End Function

    %REM
        Sub SendToBrowser
        Description: Print JSON to browser, with correct MIME type
    %END REM
    Public Sub SendToBrowser()
        '*** MIME Header to tell browser what kind of data we will return (JSON).
        '*** See http://www.ietf.org/rfc/rfc4627.txt
        Print "content-type: application/json"
        Print me.GetJSON
    End Sub

    %REM
        Sub SendJSONPToBrowser
        Description: Print JSONP to browser, with correct MIME type
    %END REM
    Public Sub SendJSONPToBrowser(callbackFunction As String)
        '*** MIME Header to tell browser what kind of data we will return (Javascript).
        '*** See http://www.rfc-editor.org/rfc/rfc4329.txt
        Print "content-type: application/javascript"
        Print callbackFunction + "(" + me.GetJSON + ")"
    End Sub

End Class

然后是一个如下函数:

class Car{
public:
  //stuff
private:
  Person* owner;
};

当函数完成时,存在内存泄漏,为了修复它,课程资料建议通过向量中的每个Car并删除函数f()末尾的所有者指针。简单地为Car写一个析构函数是不够的,它会删除它的所有者以防止内存泄漏?

只需在Car类中添加:

void f() {
  vector<Car> v;
  // do stuff with the vector
}

1 个答案:

答案 0 :(得分:6)

不,这肯定不够。你的班级是否正确遵守5的规则?即你有没有正确定义或删除复制构造函数,assingment操作符,移动构造函数和移动assingment?你真的准备好自己管理这个指针吗?

我有两个解决方案。

  1. 根本不要使用指针。从代码中不清楚为什么需要指向Person的指针,而不是Person类型的对象。您是否以多态方式呼叫Person个成员?除非必须,否则请使用非指针。
  2. 如果您确定需要指针,请使用管理自身的指针。 std::unique_ptr应该是您的第一站,如果您确定需要共享所有权,请使用std::shared_ptr