将一个类对象的指针分配给另一个类的对象c++
Assign the pointer of one class object to another class object c++
所以我对C++编程很陌生,所以如果我问了一些琐碎的问题,我会事先道歉。我的任务是对多项式进行加法、乘法和求值,其中指定多项式的每个项都由一个具有私有变量的Node类表示:double coefficient、int power和Node*next。
class Node{
private:
double coef;
int power;
Node *next;
public: blah
}
该链表的头(对于每个多项式)将存储在Poly对象的数组中,其中我的Poly类中唯一的私有变量是Node*head。
class Poly{
private:
Node *head;
public:poly functions;
}
用户要通过从我的多项式数组中选择一个元素来选择他们想要使用的多项式,这将为所选多项式提供头部。
poly_array[n];
然而,我现在的问题是,这个数组的元素是对象Poly的,我想让它成为Node类,这样我就可以实际提取它在类中的内容,并使用这种方法遍历所选多项式的节点。这是我试图实现的代码,但我对convert poly的函数调用返回了垃圾。我不知道下一步该尝试什么方法。提前谢谢。这就是我试图首先遍历多项式以显示其内容的地方。
void init_polydisplay(vector<Poly*> polynomial_array, int numofpolys)
{
Poly *polyobject;
Node *polyhead;
for (int n = 0; n < numofpolys; n++)
{
temp3.getnodehead();
polyhead=polyobject->convertPoly(polynomial_array[n]);
}
}
我试图返回Node*,而不是仅仅返回多项式的头。
Node* Poly::convertPoly(Poly* tmp)
{
return (Node *) tmp;
}
您可以在Poly
中定义get_head()
函数
class Poly{
private:
Node *head;
public:
Node * get_head()
{
return head;
}
};
并以这种方式使用:
polyhead = polynomial_array[n]->get_head();
相关文章:
- 有没有一种方法可以使用placement new将堆叠对象分配给分配的内存
- C++ 如何在将新对象分配给另一个对象时创建新对象
- 为什么我可以在不重载 "=" 运算符的情况下将一个对象分配给另一个对象?
- 为对象分配整数.输出将是什么?
- C++ 使用枚举类对象分配 std::map 值
- 如何在C++中为增加但记住删除先前对象的对象分配唯一标识符
- 我重载了 << 和 = 运算符。为什么当我将一个对象分配给另一个对象并尝试打印它时,我会被打印出来?
- 为什么将两个对象分配给另一个对象后,两个对象不一样?
- 有没有办法在同名类 (c++) 中为对象分配一个指针变量
- 如何使用函数(而不是构造函数)将派生类对象分配给基类指针
- 是否可以使用 malloc 为类对象分配内存?
- 将对象分配给数组C++
- 我们可以在cpp中为对象分配函数吗
- 为模板参数类型中的新对象分配内存
- 我无法将对象分配给对象数组
- 对象分配-成员函数的使用无效错误
- 将派生对象分配给函数内的基类指针
- Arduino ESP32如何将BLEUUID对象分配到字符串中
- Rapidjson将密钥值从一个文档对象分配到另一个文档对象
- 奇怪的对象分配行为C