我来这里是想问一些我自己无法弄清楚的事情。我一直在编写一个存储 32 位数组的小类,并且可以执行简单的数学运算,如+、和。-/*
假设我有一个像这样的类:
class Binary_Class
{
char bin[32]; // 32 bits array to hold an unsigned binary number
void set_dec(int value){}; // setting the bin[32] value based on input value
// I have operator + and = to perform on objects of this class.
Binary_Class& operator= (const Binary_Class&);
const Binary_Class operator+ (const Binary_Class&);
}
到现在为止,如果我声明了 3 个对象a, b,c的 class Binary_Class, 那么set_decto aand b, 语句c=a+b就可以使用 (?)
但是,我想通过使用一个新类来扩展类二进制文件
class Binary_Class_Extended: public Binary_Class
{
// weird functions
}
如果我声明了 3 个对象a, b,c类Binary_Class_Extended, 我还能c=a+b像以前一样使用吗?
在 Netbean 中,它说没有 operator= 与我的c=a+bif all are of匹配,Binary_Class_Extended但如果我声明该语句有效。这意味着返回 a就好像没有被带到新类中一样。cBinary_Classa+bconst Binary_Classoperator+
我是否遗漏了什么或者这就是它的方式?
当然,我可以发布整个代码,因为它只是一个作业,但我认为这些信息现在就足够了。
更新
class Binary_Class
{
char bin[32]; // 32 bits array to hold an unsigned binary number
void set_dec(int value){}; // setting the bin[32] value based on input value
//i have operator + and = to perform on objects of this class.
Binary_Class& operator= (const Binary_Class&);
const Binary_Class operator+ (const Binary_Class&) const;
}
class Binary_Class_Extended: public Binary_Class
{
// weird functions
}
当我尝试Binary_Class_Extended显示此错误的所有对象时:
main.cpp:285: 错误:'sd = ((Binary_Class*)(&sa))->Binary_Class::operator+(((const Binary_Class&)((const Binary_Class*)((Binary_Class* )(&sb)))))'
Binary_ET_Class sa,sb,sc;
sc=sa+sb//初始化sa并sb为非空值;
我一直在处理的完整源代码:https ://pastebin.com/eiVz0f5p