尝试制作类型转换操作员
Trying to make type conversion operator
我正在尝试使我的操作员起作用(有时确实如此(,但是在输出时,我会收到垃圾。
我尝试了隐式类型的转换,在indprod构造函数中设置对象f
的私有方法,没有帮助。
class FoodProd : public Exp
{
private:
int consump_term;
char *producer;
public:
FoodProd()
{
producer = new char[7];
strcpy(producer, "NONAME");
consump_term = 0;
}
FoodProd(int term, char *str)
{
int size1 = strlen(str) + 1;
producer = new char[size1];
if (producer)
strcpy(producer, str);
consump_term = term;
}
FoodProd(const FoodProd &food)
{
producer = new char[strlen(food.producer) + 1];
strcpy(producer, food.producer);
consump_term = food.consump_term;
}
~FoodProd()
{
if (producer)
delete[] producer;
}
...
}
class IndProd : public Exp
{
private:
char *category;
int price;
char *manufacturer;
public:
IndProd()
{
category = new char[7];
strcpy(category, "NONAME");
manufacturer = new char[7];
strcpy(manufacturer, "NONAME");
price = 0;
}
IndProd(char *cat, int pr, char *manuf)
{
category = new char[strlen(cat) + 1];
strcpy(category, cat);
price = pr;
manufacturer = new char[strlen(manuf) + 1];
strcpy(manufacturer, manuf);
}
IndProd(const IndProd &ind)
{
category = new char(strlen(ind.category) + 1);
strcpy(category, ind.category);
price = ind.price;
manufacturer = new char(strlen(ind.manufacturer) + 1);
strcpy(manufacturer, ind.manufacturer);
}
~IndProd()
{
if (category)
delete[] category;
if (manufacturer)
delete[] manufacturer;
}
IndProd (FoodProd obj)
{
category = new char[7];
strcpy(category, "NONAME");
manufacturer = new char[strlen(obj.get_producer()) + 1];
strcpy(manufacturer, obj.get_producer());
price = 0;
}
operator FoodProd const ()
{
FoodProd f(0, manufacturer);
return f;
}
}
Input:
FoodProd FP(20, "Ukraine");
IndProd ID("BudMat", 1900, "Germany");
FoodProd FP2;
FP2 = ID;
FP2.print();
IndProd ID2;
ID2 = FP;
Output:
Term of consumption: 0 days.
Manufacturer: ↑r;
Category: ♫ Price: 0$
Manufacturer:
答案是为两个类提供operator =
。
相关文章:
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- C++中的双指针类型转换
- 逐位操作的隐式类型转换
- 模板中的类型转换
- 在 C++(和 C)中进行类型转换时明显不一致
- 字符类型转换不兼容
- 将复杂的非基元C++数据类型转换为 Erlang/Elixir 格式,以使用 NIF 导出方法
- C++:用户定义的显式类型转换函数错误
- 将类指针类型转换为键时出错
- 通过引用传递参数时C++类型转换
- 在 C++ 中将一个模板类型的对象类型转换为另一个模板类型
- C++显式类型转换(C 样式强制转换)的强制表示法和static_cast的多种解释
- C++无效的函数类型转换
- 在将派生类指针类型转换为派生类指针后,从基类指针调用派生类函数
- 如何将Windows产品类型转换为名称?
- 通过构造函数方法输出的类到类类型转换是 5500 为什么不是 5555
- 尝试制作类型转换操作员
- 为什么C 标准要求编译器忽略对基本类型转换操作员的呼叫
- 模板参数类型通过转换操作员扣除