簡體   English   中英

C ++為堆棧類創建副本構造函數

[英]C++ Creating a copy constructor for stack class

我定義了一個堆棧類,其中包含用於將值壓入和彈出到堆棧的方法。

在測試器文件(如下所示)中,運行該文件后,會發生並導致程序崩潰。 我知道這是由於函數f引起的,因為兩個指針指向內存中的同一位置,所以會產生錯誤。 如果我在調用函數時注釋掉f(s)行,則pop&push函數可以正常工作,並且輸出正確。

為了解決這個錯誤,我被要求 為此類創建一個副本構造函數以解決上述問題。

我對此並不十分熟悉,因此在執行此操作方面會有所幫助。 謝謝

主測試文件

#include "Stack.h"
#include <iostream>
#include <string>
using namespace std;

void f(Stack &a) {
    Stack b = a;
}


int main() {

    Stack s(2); //declare a stack object s which can store 2 ints
    s.push(4); //add int 4 into stack s

    //s = [4]
    s.push(13); //add int 13 into stack s
    //s = [4,13]

    f(s); //calls the function f which takes in parameter Stack a , and sets Stack b = to it.
    //error here - as 2 pointers point to the same location in memory !
    cout << s.pop() << endl; //print out top element(most recently pushed) element.
    //so should output 13
    return 0;
}

頭文件代碼

#ifndef STACK_H
#define STACK_H

class Stack {
public:
    //constructor
    Stack(int size);

    //destructor
    ~Stack();

    //public members (data & functions)
    void push(int i);
    int pop();

private:
    //private members (data & functions)
    int stck_size;
    int* stck;
    int top;
};

#endif

Stack.cpp代碼

#include "Stack.h"
#include <iostream>
#include <string>
using namespace std;

Stack::Stack(int size){
    stck_size = size;
    stck = new int[stck_size];
    top = 0;
}
Stack::~Stack() {
    delete[] stck;
}
void Stack::push(int i) {
    if (top == stck_size) {
        cout << "Stack overflow." << endl;
        return;
    }
    stck[top++] = i;
}

int Stack::pop() {
    if (top == 0) {
        cout << "Stack underflow." << endl;
        return 0;
    }
    top--; //decrement top so it points to the last element istead of the empty space at the top.
    return stck[top];
}

復制構造函數非常快捷而且骯臟:

Stack::Stack(const Stack & src): 
    stck_size(src.stack_size),
    stck(new int[stck_size]),
    top(src.top) //Member Initializer List
{
    // copy source's stack into this one. Could also use std::copy.
    // avoid stuff like memcpy. It works here, but not with anything more 
    // complicated. memcpy is a habit it's just best not to get into
    for (int index = 0; index < top; index++)
    {
        stck[index] = src.stck[index];
    }
}

現在,您已經有了一個復制構造函數,您可能仍然會感到困惑,因為三分法則還沒有得到滿足。 您需要operator= 這很容易,因為復制構造和復制和交換習慣使它變得容易。

基本形式:

TYPE& TYPE::operator=(TYPE rhs) //the object to be copied is passed by value
                                // the copy constructor makes the copy for us.
{
  swap(rhs); // need to implement a swap method. You probably need one 
             //for sorting anyway, so no loss.
  return *this; // return reference to new object
}

您的副本構造函數應如下所示:

Stack::Stack(const Stack &r) {
    stck_size = r.stck_size;
    stck = new int[stck_size];
    top = r.top;
    memcpy(stck, r.stck, top*sizeof (int));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM