简体   繁体   English

C ++:指针和抽象数组类

[英]C++: pointers and abstract array classes

I am relatively new to pointers and have written this merge function. 我对指针还比较陌生,并且已经编写了此合并功能。 Is this effective use of pointers? 这是有效使用指针吗? and secondly the *two variable, it should not be deleted when they are merged right? 第二个*two变量,合并时不应该删除它吗? that would be the client´s task, not the implementer? 那将是客户的任务,而不是实施者的任务?

VectorPQueue *VectorPQueue::merge(VectorPQueue *one, VectorPQueue *two) {
int twoSize = two->size();

if (one->size() != 0) {
    for (int i = 0; i < twoSize;i++)
    {
        one->enqueue(two->extractMin());
    }
}
return one;

} }

The swap function is called like this 交换函数这样调用

one->merge(one, two);

Passing it the these two objects to merge 将这两个对象传递给它以进行合并

PQueue *one = PQueue::createPQueue(PQueue::UnsortedVector);
PQueue *two = PQueue::createPQueue(PQueue::UnsortedVector);

In your case pointers are completely unnecessary. 在您的情况下,完全不需要指针。 You can simply use references. 您可以简单地使用引用。

It is also unnecessary to pass in the argument on which the member function is called. 也不必传入调用成员函数的参数。 You can get the object on which a member function is called with the this pointer. 您可以使用this指针获取在其上调用成员函数的对象。

/// Merge this with other.
void VectorPQueue::merge(VectorPQueue& other) {
  // impl
}

In general: Implementing containers with inheritance is not really the preferred style. 通常,使用继承实现容器并不是真正的首选样式。 Have a look at the standard library and how it implements abstractions over sequences (iterators). 看一下标准库及其在序列上实现抽象的方法(迭代器)。

At first sight, I cannot see any pointer-related problems. 乍一看,我看不到任何与指针相关的问题。 Although I'd prefer to use references instead, and make merge a member function of VectorPQueue so I don't have to pass the first argument (as others already pointed out). 尽管我更喜欢使用引用,并且使merge作为VectorPQueue的成员函数,所以我不必传递第一个参数(正如其他人已经指出的那样)。 One more thing which confuses me is the check for one->size() != 0 - what would be the problem if one is empty? 使我感到困惑的另一件事是对one->size() != 0 -如果one为空,那会是什么问题? The code below would still correctly insert two into one , as it depends only on two 's size. 下面的代码仍然可以正确地将two插入one ,因为它仅取决于two的大小。

Regarding deletion of two : 关于删除two

that would be the client´s task, not the implementer 那将是客户的任务,而不是实施者

Well, it's up to you how you want do design your interface. 好吧,由您决定如何设计界面。 But since the function only adds two 's elements to one , I'd say it should not delete it. 但是由于该函数仅将two的元素添加到one元素中,所以我说它不应删除它。 Btw, I think a better name for this method would be addAllFrom() or something like this. 顺便说一句,我认为这种方法的更好的名字是addAllFrom()或类似的东西。

Regarding pointers in general : 关于一般指针

I strongly suggest you take a look into smart pointers. 我强烈建议您研究一下智能指针。 These are a common technique in C++ to reduce memory management effort. 这些是C ++中减少内存管理工作量的常用技术。 Using bare pointers and managing them manually via new/delete is very error-prone, hard to make strongly exception-safe, will almost guarantee you memory leaks etc. Smart pointers on the other hand automatically delete their contained pointers as soon as they are not needed any more. 使用裸指针并通过new / delete手动管理它们很容易出错,很难保证异常安全性,几乎可以保证您发生内存泄漏等。另一方面,智能指针会自动删除其包含的指针。需要更多。 For illustrative purposes, the C++ std lib has auto_ptr ( unique_ptr and shared_ptr if your compiler supports C++ 11). 出于说明目的,C ++ std库具有auto_ptr (如果您的编译器支持C ++ 11,则为unique_ptrshared_ptr )。 It's used like this: 它的用法如下:

{ // Beginning of scope
std::auto_ptr<PQueue> one(PQueue::createPQueue(PQueue::UnsortedVector));

// Do some work with one...:
one->someFunction();
// ...

} // End of scope - one will automatically be deleted

My personal rules of thumb: Only use pointers wrapped in smart pointers. 我个人的经验法则:仅使用包装在智能指针中的指针。 Only use heap allocated objects at all, if: 在以下情况下,仅使用堆分配的对象:

  • they have to live longer than the scope in which they are created, and a copy would be too expensive (C++ 11 luckily has move semantics, which eliminate a lot of such cases) 它们的生存期必须超过其创建范围,并且副本将太昂贵(幸运的是,C ++ 11具有移动语义,从而消除了许多此类情况)
  • I have to call virtual functions on them 我必须在它们上调用虚函数

In all other cases, I try to use stack allocated objects and STL containers as much as possible. 在所有其他情况下,我尝试尽可能使用堆栈分配的对象和STL容器。

All this might seem a lot at first if you're starting with C++, and it's totally ok (maybe even necessary) to try to fully understand pointers before you venture into smart pointers etc.. but it saves a lot of time spend debugging later on. 如果您从C ++开始,所有这些乍一看似乎很多,而且完全可以(甚至有必要)在尝试使用智能指针等之前尝试完全理解指针。但是,这样可以节省大量时间,以后再进行调试上。 I'd also recommend reading a few books on C++ - I was actually thinking I understood most of C++, until I read my first book :) 我还建议您阅读几本有关C ++的书-实际上,我以为我了解大多数C ++,直到我读了第一本书:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM