重载“=” 使 obj2 = obj1 时不会调用相等性
我有一个名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
可能是一个技术问题,但您不是分配一个
ClassA
,而是分配一个ClassA*
(即指针)。 我可能离这里很远,但这就是我应该归咎的地方。Might be a technicality, but you're not assigning a
ClassA
, you're assigning aClassA*
(ie, a pointer). I might be way off here, but this is where I'd lay the blame.我怀疑您正在使用 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.
在您的代码中,您已经为采用
void*
的MemoryManager
类定义了operator=
。您的示例代码正在初始化
ClassA
指针,而不是分配给MemoryManager
实例。您的代码没有被调用有三个原因。
ClassA
,而不是您实际为其提供了operator=
的MemoryManager
。From you code, you have defined
operator=
for theMemoryManager
class taking avoid*
.Your example code is initializing
ClassA
pointers and not assigning toMemoryManager
instances.There are three reasons why your code is not being called.
ClassA
and notMemoryManager
which you have actually provided theoperator=
for.请参阅规范,您不能覆盖指针基本操作。
See spec, you cannot override pointer basic operations.