Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 当数组是类的数据成员时,如何使用指针交换数组_C++_Arrays_Oop_Pointers - Fatal编程技术网

C++ 当数组是类的数据成员时,如何使用指针交换数组

C++ 当数组是类的数据成员时,如何使用指针交换数组,c++,arrays,oop,pointers,C++,Arrays,Oop,Pointers,我试图通过交换指向两个数组的指针来交换数组中的内容 我的方法与Daniel在这个问题中的回答相同:。但区别在于我的数组将是类中的成员。 我的代码可以成功编译,但是输出结果很奇怪 这是我的头文件: #include <stdio.h> #include <iostream> class Map { public: Map(int times); // Create an empty map (i.e., one with no key/value pairs)

我试图通过交换指向两个数组的指针来交换数组中的内容

我的方法与Daniel在这个问题中的回答相同:。但区别在于我的数组将是类中的成员。 我的代码可以成功编译,但是输出结果很奇怪

这是我的头文件:

#include <stdio.h>
#include <iostream>

class Map
{
public:
    Map(int times); // Create an empty map (i.e., one with no key/value pairs)

    int size();     // Return the number of key/value pairs in the map.
    void dump();
    void swap(Map &other);
    int *retrieve();
    void setptr(int *newptr);

private:
    int *ptr;
    int array_1[5];
};
#包括
#包括
类图
{
公众:
映射(int次);//创建一个空映射(即没有键/值对的映射)
int size();//返回映射中键/值对的数目。
无效转储();
无效掉期(Map和其他);
int*retrieve();
void setptr(int*newptr);
私人:
int*ptr;
int数组_1[5];
};
以下是我的实现:

#include "Map.h"
#include <iostream>

using namespace std; 

Map::Map(int times) {
    for (int i = 0; i < 5; i++) {
        array_1[i]=i*times;
    }
    ptr=array_1;
}

void Map::dump() {
    ptr=array_1;
    for (int i = 0; i < 5; i++) {
        cout << *ptr << endl;
        ptr++;
    }
    for (int i = 0; i < 5; i++) {
        ptr--;
    }  
}

void Map::swap(Map &other) {
    int *temp;
    temp = this->ptr;
    this->ptr = other.retrieve();
    other.setptr(temp);
}

int *Map::retrieve() {
    return ptr;
}

void Map::setptr(int *newptr) {
    ptr=newptr;
}
#包括“Map.h”
#包括
使用名称空间std;
Map::Map(整数倍){
对于(int i=0;i<5;i++){
数组_1[i]=i*次;
}
ptr=阵列_1;
}
void映射::dump(){
ptr=阵列_1;
对于(int i=0;i<5;i++){
cout ptr=other.retrieve();
其他。设置PTR(温度);
}
int*Map::retrieve(){
返回ptr;
}
无效映射::setptr(int*newptr){
ptr=newptr;
}

有人能告诉我哪里出了问题,以及如何巧妙地实现它吗?

您的设计的问题是指针指向同一对象中的数组

假设必须交换对象a和b。如果交换它们的指针,a.ptr将指向包含数据的b.array_1。反过来,b.ptr将指向a.array1

不幸的是,如果其中一个对象(比如b)被破坏(因为它是一个超出范围的本地对象,或者出于任何原因),剩余对象的指针将指向一个不再存在的数组。这就是UB

为了解决您的问题,您需要在构造函数中动态分配一个数组。完全摆脱数组_1:

Map::Map(int times){
    ptr=new int[5];  // or better define a constant to avoid hard coded sizes        
    for (int i=0;i<5;i++){
        ptr[i]=i*times;
    }
}
避免此类问题的一个简单技巧是系统地将不应更改对象状态的成员函数声明为常量:

class Map {
   ...
   void dump() const;
   ...
}

如果您试图意外更改成员,编译器将发出错误。

以下代码运行正常:

#include <stdio.h>
#include <iostream>
#include <conio.h>
using namespace std; 
class Map
{
public:
    Map(int times);         // Create an empty map (i.e., one with no key/value pairs)
    int size();    // Return the number of key/value pairs in the map.
    void dump();
    void swap(int &other);
    int *retrieve();
    void setptr(int *newptr);
private:
    int *ptr;
    int array_1[5];
};


Map::Map(int times){
    for (int i=0;i<5;i++){
        array_1[i]=i*times;
    }
    ptr=array_1;
}

void Map::dump(){
    for (int i=0;i<5;i++)
    {
        cout<<ptr[i]<<endl;        
    }

}

void Map::swap(int &other){
    int *temp;
    temp=this->ptr;
    this->ptr=&other;
    other = *temp;
}

int *Map::retrieve(){
    return ptr;
}

void Map::setptr(int *newptr){
    ptr=newptr;
}

int main()
{
    Map m(2);
    Map n(3);
    m.dump();
    m.swap(*n.retrieve());
    m.dump();
    getchar();
}

注意:它可能并不优雅。

那些“怪异”是什么结果?在另一个
Map
中设置指向数组的指针意味着取得该数组的所有权,这意味着你取得了
Map
的所有权。John,我使用dump函数使用ptr*输出数组。输出为0 4528 1 1969619704 32767jxh,取得另一个Map的所有权是错误的吗将
ptr
移动到最后,再也不放回去…Christophe,谢谢你的建议。但这是一个课程问题,指令不允许我们使用动态分配数组。此外,我的问题发生在一个对象被破坏之前。好的,我明白了,我已经为你代码中的另一个问题进行了编辑!谢谢你的帮助你的代码。它解决了我的问题。但我的编码指令要求使用与它提供的签名完全相同的签名。我必须坚持使用void swap(映射和其他)。这是一个家庭作业问题吗?我想可能需要交换两个映射指针。是的,这是家庭作业。以下是要求。void swap(映射和其他);//将此映射的内容与另一个映射交换。交换函数很容易实现,无需创建任何附加数组或附加映射。您认为交换映射指针是更好的解决方案吗?是的,当然,否则一个映射中的指针将指向另一个映射中的数组。您必须交换映射指针。a还有其他要求吗?请在问题中列出所有要求!
#include <stdio.h>
#include <iostream>
#include <conio.h>
using namespace std; 
class Map
{
public:
    Map(int times);         // Create an empty map (i.e., one with no key/value pairs)
    int size();    // Return the number of key/value pairs in the map.
    void dump();
    void swap(int &other);
    int *retrieve();
    void setptr(int *newptr);
private:
    int *ptr;
    int array_1[5];
};


Map::Map(int times){
    for (int i=0;i<5;i++){
        array_1[i]=i*times;
    }
    ptr=array_1;
}

void Map::dump(){
    for (int i=0;i<5;i++)
    {
        cout<<ptr[i]<<endl;        
    }

}

void Map::swap(int &other){
    int *temp;
    temp=this->ptr;
    this->ptr=&other;
    other = *temp;
}

int *Map::retrieve(){
    return ptr;
}

void Map::setptr(int *newptr){
    ptr=newptr;
}

int main()
{
    Map m(2);
    Map n(3);
    m.dump();
    m.swap(*n.retrieve());
    m.dump();
    getchar();
}
void Map::swap(Map &other){
    Map *temp;
    temp=this;
    *this = other;
    other = *temp;
}

Map *Map::retrieve(){
    return this;
}