gpt4 book ai didi

c++迭代基类和派生类

转载 作者:行者123 更新时间:2023-11-30 03:16:37 26 4
gpt4 key购买 nike

我试图找到一种方法让迭代器处理自定义对象列表和从该自定义对象派生的对象列表。我的目标(也许被误导了)是让“生产”代码和对象完好无损,但可以从我正在尝试的“实验性/扩展”事物中访问。

这是我正在尝试做的一个非常简单的例子。

#include <iostream>
#include <list>
using std::cout;
using std::endl;
using std::cin;
using std::list;

struct comp{
double x,y;

void print(){
cout << "x: " << x << endl;
cout << "y: " << y << endl;
}
comp(){
x = 0;
y = 0;
}
comp(double X, double Y){
x = X;
y = Y;
}
// Standard/Tested Member Functions
};

struct base{
list<comp> components;
double known, tested, variables;

void print_comps(){
for (list<comp>::iterator it = components.begin(); it != components.end(); ++it){
// Ideally, this function should work for comp1 and comp1x
// as inherited in the basex class
it->print();
}
}
// Standard/Tested Member Functions
};

struct compx : comp{
double w,z;
double some, alter, nates;

void print(){
cout << "x: " << x << endl;
cout << "y: " << y << endl;
cout << "w: " << w << endl;
cout << "z: " << z << endl;
}
compx(){
x = 0;
y = 0;
z = 0;
w = 0;
}
compx(double X, double Y, double Z, double W){
x = X;
y = Y;
z = Z;
w = W;
}
// Experimental/Un-tested Member Functions
};

struct basex : base{
list<compx> components;
double exper, imen, tal;

// void print_comps(){} // This should be inherited from base

// Experimental/Un-tested Member Functions
};

int main(){

base compilation1;
compilation1.components.push_back(comp(1,2));
compilation1.components.push_back(comp(3,4));
cout << "printing normal struct" << endl;
compilation1.print_comps();

cout << endl;

basex compilation2;
compilation2.components.push_back(compx(9, 5, 5, 6));
compilation2.components.push_back(compx(7, 2, 1, 8));
cout << "printing extended struct" << endl;
compilation2.print_comps(); // Prints nothing

cout << endl;

cout << "Printing via specific iterator" << endl;
for (list<compx>::iterator it = compilation2.components.begin(); it != compilation2.components.end(); ++it){
it->print(); // Works as expected.
}

cout << endl << endl << "Press ENTER to exit." << endl; cin.get();
return 0;
}

理想情况下,我将能够在相同的函数中迭代原始类和扩展类,这样我就不会将所有原始代码混杂在扩展类中。这将允许我简单地将代码从扩展类移动到原始类,因为这些变量或函数已被证明或成熟。

背景:

  • 我不喜欢列表——任何其他可迭代的类都可以。
  • 我不是开发人员 -- 我是一名 ChemE,试图在不破坏我已经构建的内容的情况下让日常任务变得更简单。
  • git 存储库中的分支不是一个很好的解决方案,因为其他非开发人员、潜在的代码挑战者可能会尝试扩展它。让他们使用甚至一个分支机构都是一个奇迹。
  • 我在 Linux (Lubuntu) 上使用 g++ 7.4.0,在 Windows 7 上使用 6.3.0。

长话短说:

有没有办法得到list<parent_object>的迭代器?也遍历 list<child_object>

最佳答案

这可以通过将base 作为模板来解决http://cpp.sh/7r2x6a

template<typename T>
struct base
{
list<T> components;
double known, tested, variables;

void print_comps(){
for (auto it = components.begin(); it != components.end(); ++it){
// Ideally, this function should work for comp1 and comp1x
// as inherited in the basex class
it->print();
}
}
// Standard/Tested Member Functions
};

关于c++迭代基类和派生类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56195443/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com