简体   繁体   English

C ++指针和类

[英]C++ pointers and classes

Trying to solve this question for the last 45 minutes, but couldn't figure out the what I'm suppose to do. 尝试在最后45分钟解决这个问题,但无法弄清楚我想要做什么。 Here's the question. 这是问题所在。

class C; //forward declaration
class Cpointer {
 public:

};

class C { //do not modify
   int i;
  public:
   C(int _i=0) : i(_i) {}
   Cpointer operator& () { return Cpointer(this); }
   void Do() { std::cout << "i=" << i << std::endl; }
 };


 int main() {
   Cpointer p (new C(100));
   p->Do();
 }

Complete class Cpointer so that the code compiles. 完成类Cpointer以便代码编译。 Make sure there are no memory leaks. 确保没有内存泄漏。

As I understand CPointer class takes a C class as a parameter for the constructor. 据我所知,CPointer类将C类作为构造函数的参数。 When I try the below code I'm getting a compiler error telling that "use of undefined type C" inside the CPointer's Do function. 当我尝试下面的代码时,我得到一个编译器错误,告诉CPointer的Do函数中“使用未定义的类型C”。

class Cpointer
{
 C* mCptr;
   public:
  Cpointer(C* cptr) : mCptr(cptr){}
  void Do()
  {
    //mCptr->Do();
  }
};

I feel slightly uneasy posting solution but since you say it's not a homework... Here it is: 我觉得稍微不安的发布解决方案,但因为你说这不是一个功课......这里是:

class Cpointer {
    C* ptr;
    public:
        Cpointer(C* c) : ptr(c) { }
        ~Cpointer() { delete ptr; }
        C* operator->() { return ptr; }
};

There are three points here: 这里有三点:

  1. Constructor to get the C* pointer and store it in the improvided pointer-like class. 构造函数获取C*指针并将其存储在改进的类指针类中。
  2. Destructor to remove the object heap-allocated C when p gets out of scope of main function. p超出main函数的范围时,析构函数删除对象堆分配的C
  3. Overloaded -> operator so that p->Do(); 重载->运算符使p->Do(); will correctly invoke the C::Do method. 将正确调用C::Do方法。

Basically, what you were asked to implement is a partial functionality of the standard auto_ptr class. 基本上,您要求实现的是标准auto_ptr类的部分功能。

Given the way Cpointer is used in main(), it has to be implemented like this: 考虑到在main()中使用Cpointer的方式,它必须像这样实现:

class Cpointer
{
private:
    C* c:
public:
    Cpointer(C* _c) : c(_c) {}
    ~Cpointer() { delete c; }
    C* operator ->() { return c; }
};

The problem is that Cpointer is also used by C's overriden '&' operator, and this kind of implementation will not work for that usage. 问题是C的覆盖'&'运算符也使用Cpointer,这种实现不适合这种用法。 Memory corruption will occur, especially if the C instance is declared on the stack (which is when an overriden '&' operator is usually needed). 内存损坏将发生,特别是如果在堆栈上声明C实例(通常需要覆盖'&'运算符)。 To really make this work correctly, C would have to be modified, either to stop using Cpointer, or to contain a reference count that Cpointer manages. 要真正使这项工作正常,必须修改C,要么停止使用Cpointer,要么包含Cpointer管理的引用计数。 Either way violates the "do not modify" restriction on C. 无论哪种方式都违反了对C的“不要修改”限制。

so that it compiles, and doesn't leak? 这样它编译,不泄漏? ok. 好。

class Cpointer
{
   public:
   Cpointer(C* c)
   {  delete c; }
  void Do()
  {  }
   Cpointer* operator ->()
   { return this; }
};
class C; //forward declaration
class Cpointer {
 public:
 C* mptr;

 Cpointer(C *cptr){
     mptr = cptr;
 }

 ~Cpointer(){
     delete mptr;
 }

 C* operator->() const {
     return mptr;
 }
};

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

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