分配不同的类对象时分配错误

时间:2015-03-10 16:41:17

标签: c++ variable-assignment operator-keyword

我在类b中重载了赋值运算符但是 执行下面的代码片段后,我收到了编译错误:

error:
cannot convert 'a*' to 'b*' in assignment
  bobj=aobj;
 ^

由于此错误,似乎bobj->showname();似乎不起作用,因为aobj的内容仍未复制到bobj且仍然表现为b类的目标。我从来没有能够在不同类型的对象之间执行任务。我相信可以分配不同类别的对象。

代码段:

#include <iostream>
using namespace std;
class a {
    int a1;
public:
    a(int _a1 = 0) : a1(_a1) {}
    void showname() {
        cout << "class name a with value =" << a1 << endl;
    }
};

class b {
    public:
        const b& operator=(const b& rhs);
    private:
        a* cobj;
};

const b& b::operator=(const b& rhs) {
    a* origobj = cobj;
    cobj = new a(*rhs.cobj);
    delete origobj;
    return *this;
}

int main() {
    a* aobj = new a;
    b* bobj = new b;
    bobj = aobj; //error during assignment 
    bobj->showname();
    return 0;
}      

//problem section updated with other scenario
  class Bitmap {};
  class widget
  {
  public:
  const widget& operator=(widget & rhs)
 {
 Bitmap *origobj=obj;// backup of original pb
 obj=new widget (*rhs.obj);//make pb point oto a copy of *pb
 delete origobj;//delete the original pb
 return *this
 }

private:
Bitmap *obj;
 };

5 个答案:

答案 0 :(得分:2)

请参阅以下链接,了解代码段作为问题的解决方案。我完全同意你必须使用智能指针,但正如你所说,由于你的应用程序的一些限制,它是不允许的,你没有任何选择:)。

链接:https://ideone.com/wq9kdO

代码:

#include<iostream>

using namespace std;
class a 
{
int a1;
public:
a( int _a1=0):a1(_a1)
{
}
void showname()
{
cout<<"class name a with value ="<<a1<<endl;    
}
};

class b
{
public:
b(a* cobj) : cobj(cobj)
{
cout<<"object initialization\n";    
}
void display()
{
 cobj->showname();  
}

private:
a *cobj;    
};

int main() {
a aobj(10);
b bobj(&aobj);  
bobj.display(); 
}

说明:如果两个类之间没有关系,但仍然有一个类对其他类的某些功能感兴趣,那么解决方案应该是私有继承或组合。在你的场景中,我不认为私有继承似乎是可行的,因为你没有覆盖公共/受保护的虚拟功能,所以你只需要使用组合。

答案 1 :(得分:1)

众所周知,我所分享的答案并不代表现代c ++标准。在提问者的要求下,我做了同样的旧方式。下面是我根据现代c ++标准给出的答案,我对于现代c ++时代的新手程序员来说是一个很好的学习课程。

link:https://ideone.com/efdU2l

#include <iostream>
#include<memory>
using namespace std;
class a 
{
int a1;
public:
a( int _a1=0):a1(_a1)
{

}
void showname()
{
cout<<"class name a with value ="<<a1<<endl;  
}
};
class b
{
public:
b(std::unique_ptr<a>cobj) : cobj( std::move(cobj))
{
cout<<"object initialization\n";  
}
void display()
{
 cobj->showname();  
}
~b ()
{
cout<<"deleted the object of a"<<endl;

}
private:
std::unique_ptr<a> cobj;    
};

int main() {
std::unique_ptr<a> aobj (new a (10)) ;
b bobj(std::move(aobj));  
bobj.display(); 
}

答案 2 :(得分:1)

您需要正确实施赋值运算符。下面是相同的代码片段。

const b& b::operator=(const a& rhs) {

if(&rhs!=cobj)
  {
   delete cobj;
   cobj=new a(rhs);
   }
    return *this;
}

现在为他们分配*bobj = *aobj;

答案 3 :(得分:0)

您需要将aobj包裹在b中,因为b的赋值运算符只能理解b类型的参数。如果向b添加一个可以接受a类型参数的构造函数,那么它将使用该构造函数将aobj强制转换为b。这将解决您的问题。

答案 4 :(得分:0)

a与b无关。这就像在做

std::string* str_p = new std::string("Hello");
std::vector<double>* vec_p = new std::vector<double>();
str_p = vec_p;  // doesn't make sense. 

另外,请遵循Mikes的建议:远离指针。

另一方面,如果std :: vector包含一个字符串,则该向量允许您操作该值;或者如果 b 包含 a 。您需要让 b 允许用户设置 a 。这不是通过重载赋值运算符来完成的。也许像是

a obj;
b b_object;
b_object.set_a(obj);