重载“=” 使 obj2 = obj1 时不会调用相等性

发布于 2024-08-01 16:39:33 字数 1162 浏览 3 评论 0原文

我有一个名为 MemoryManager 的类,

它应该实现一个简单的智能指针,(计数引用);
我有一个向量,在其中存储请求的指针,并将指针的索引返回给调用者。

当用户创建 MemoryManager 类型的指针时,他调用名为 modded_malloc(size_t) 的初始化函数,创建一个 MemoryManager obj,分配一个内存空间并将其存储到数据中,增加计数,并将对象存储到 global_MM_vecotr 中,并将索引作为指针返回,当使用尝试使用间接寻址(->)时,我从向量中返回适当的实际指针,根据到索引值..

class MemoryManager
{
public:
    //operators overloading prototypes

private:
    void*  data;
    int count ;
};

 std::vector<MemoryManager*> global_MM_vecotr; 
  void* MemoryManager::operator=( void* x)
 {
   // some code here
 }

我面临的问题是我重载了几个运算符,但是当我尝试运行“=”运算符下面的代码时不会被调用.. 有人可以向我指出问题吗?

  //the main code 
 {

MemoryManager* obj1 = (MemoryManager*) x->fun1(4); //fun1 returns an index to a MemoryManager obj in a vector;
MemoryManager* obj2 =  obj1 ;
   }

编辑:已经尝试了以下操作,没有任何改变

  {  
        MemoryManager*obj1 = (MemoryManager*) x->fun1(4); //fun1 returns an index to a Class obj in a vector;
MemoryManager*obj2 ;
*obj2 =  *obj1;
  }


 {
     MemoryManager* obj1  = ( MemoryManager*) x-> fun1(4);
MemoryManager* obj2;
obj2.operator =(*obj1);
}

i have this class called MemoryManager,

it is supposed to implement a simple smart pointer, (count reference);
i have a vector where i store the requested pointers,and i return the index of the pointer to the caller..

when a user creates a pointer of type MemoryManager he calls an initializer function called modified_malloc(size_t) , create a MemoryManager obj, alloc a memory space and store it into data,increase count, and store the object into global_MM_vecotr , and return the index as a pointer , when the use tries to use indirection ( ->) i return the appropriate real pointer from the vector, according to the index value..

class MemoryManager
{
public:
    //operators overloading prototypes

private:
    void*  data;
    int count ;
};

 std::vector<MemoryManager*> global_MM_vecotr; 
  void* MemoryManager::operator=( void* x)
 {
   // some code here
 }

the problem i am facing is that i overloaded a couple of operators, however when i try to run the code below the "=" operator doesn't get called..
can some1 point the problem out to me..

  //the main code 
 {

MemoryManager* obj1 = (MemoryManager*) x->fun1(4); //fun1 returns an index to a MemoryManager obj in a vector;
MemoryManager* obj2 =  obj1 ;
   }

Edit: already tried the following , no change

  {  
        MemoryManager*obj1 = (MemoryManager*) x->fun1(4); //fun1 returns an index to a Class obj in a vector;
MemoryManager*obj2 ;
*obj2 =  *obj1;
  }


 {
     MemoryManager* obj1  = ( MemoryManager*) x-> fun1(4);
MemoryManager* obj2;
obj2.operator =(*obj1);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

心安伴我暖 2024-08-08 16:39:33

可能是一个技术问题,但您不是分配一个 ClassA,而是分配一个 ClassA*(即指针)。 我可能离这里很远,但这就是我应该归咎的地方。

Might be a technicality, but you're not assigning a ClassA, you're assigning a ClassA* (ie, a pointer). I might be way off here, but this is where I'd lay the blame.

挖鼻大婶 2024-08-08 16:39:33

我怀疑您正在使用 void 指针,以便您可以输入任何类型的对象。 我建议使用 模板boost::check 库

I suspect you're using the void pointer so that you can enter any kind of object. I'd recommend using a template instead combined with the boost::check library.

累赘 2024-08-08 16:39:33

在您的代码中,您已经为采用 void*MemoryManager 类定义了 operator=

您的示例代码正在初始化 ClassA 指针,而不是分配给 MemoryManager 实例。

您的代码没有被调用有三个原因。

  • 您正在初始化而不是分配,因此如果有任何情况,将调用构造函数而不是赋值运算符。
  • 您正在初始化指针而不是对象,指针是基本类型,您不能为它们提供重载运算符。
  • 您使用的是 ClassA,而不是您实际为其提供了 operator=MemoryManager

From you code, you have defined operator= for the MemoryManager class taking a void* .

Your example code is initializing ClassA pointers and not assigning to MemoryManager instances.

There are three reasons why your code is not being called.

  • You are initializing not assigning, so if anything a constructor would be called rather than an assignment operator.
  • You are initializing pointers and not objects, pointers are basic types and you cannot provide overloaded operators for them.
  • You are using ClassA and not MemoryManager which you have actually provided the operator= for.
怪我鬧 2024-08-08 16:39:33

请参阅规范,您不能覆盖指针基本操作。

See spec, you cannot override pointer basic operations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文