如何从列表中存储的对象调用成员函数
How to call member functions from objects stored in a list
我在列表中存储了几个对象。如何调用列表中对象的成员函数?
我需要访问列表中存储的每个对象,并调用每个对象以对每个对象进行分类。如果何时要做这件事要比比较每个对象的x值并在列表中交换其位置更好,我不确定如何做。
#include <iostream>
#include <string>
#include <fstream>
#include <list>
#include <algorithm>
#include <conio.h>
#define sz 12
using namespace std;
class Point
{
private:
int x, y;
public:
Point() { }
Point(int a, int b)
:x(a), y(b) { }
// print function is pure virtual and that makes class Point an abstract class
// a pure virtual function can have prototype only without definition
// an abstract class can't be instantiated
// its derived class must override this function in order to be a real class
virtual void print() const = 0;
int getX(); // added function to get x value
int getY(); // function to get Y value
};
void Point::print() const
{
cout << "nPoint: ( "
<< x
<< " , "
<< y
<< " )";
}
int Point::getX()
{
return x;
}
int Point::getY()
{
return y;
}
///////////////////////////////////////////////////////////////////
class Circle : public Point
{
private:
int radius;
public:
Circle() : Point() { }
Circle(int a, int b, int c)
:Point(a, b), radius(c) { }
virtual void print() const;
};
void Circle::print() const
{
cout << "nCenter of the Circle is at: ";
Point::print();
cout << "nRadius of the Circle is: "
<< radius;
cout << endl; // inserted endl
}
/////////////////////////////////////////////////////////////////////
class Cylinder : public Circle
{
private:
int height;
char color[sz];
public:
Cylinder() { }
Cylinder(int a, int b, int r, int h, char clr[])
: Circle(a, b, r), height(h)
{ strcpy(color, clr); }
virtual void print() const;
};
void Cylinder::print() const
{
Circle::print();
cout << "nHeight of Cylinder is: "
<< height
<< "nColor of Cylinder is: "
<< color;
Point::print();
cout << endl;
}
void load_list(list<Point*>&, char*); // Create list of shapes
void output(Point*&); // display function for for_each
///////////////////////////////////////////////////////////////
int main()
{
char clr[10];
list<Point*> shapeList;////
list<Point*>::iterator it;
load_list(shapeList, clr);
for_each(shapeList.begin(), shapeList.end(), output);
shapeList.sort();
cout << "nn///////////////////n"
<< "////////////////////n "
<< "After list is Sorted: n";
for_each(shapeList.begin(), shapeList.end(), output);
_getch();
return 0;
}
void load_list(list<Point*>& ptList, char *ch)
{
char type;
int x, y, r, h;
ifstream infile("shapes.txt");
if (!infile)
{
cout << "nCan not open input file.";
exit(1);
}
infile >> type;
while (infile)
{
if (type == 'c')
{
infile >> x >> y >> r;
ptList.push_back(new Circle(x,y,r));
}
else if (type = 'l')
{
infile >> x >> y >> r >> h >> ch;
ptList.push_back(new Cylinder(x, y, r, h, ch));
}
infile >> type;
}
}
void output(Point*& point)
{
point->print();
}
void sort_list_by_x(list<Point*>& pt)
{
list<Point*>::iterator it;
list<Point*>::iterator it2;
auto it2 = pt.begin();
auto it = it2++;
auto e = pt.end();
it->getX;
}
您需要使用比较器函数根据某些自定义标准对列表进行排序。例如,如果要对x
进行排序,则应制作一个全局函数:
bool compare_x (const Point* first, const Point* second)
{
return (first -> getX() < second -> getX());
}
然后调用sort
函数:
shapeList.sort(compare_x);
这将根据x
的值对列表进行排序。如果您使用的C++
大于C++11
,则也可以使用lambda功能:
shapeList.sort([](const Point* first, const Point* second) {
return (first -> getX() < second -> getX());
});
在这种情况下,您不必在这种情况下做出显式比较函数。
您需要确保成员函数是公开的,使用迭代器访问列表的元素。
std::list<yourObj> yourList = { foo, bar};
std::list<yourObj>::iterator it = yourList.begin();
int n = 0; //gives the first element of the list
std::advance(it, n);
(*it).memberFunc();
编辑您可以使用列表成员排序来比较这样的对象:
pt.sort([](Point* a, Point* b){return a->getX() > (b->getX());});
相关文章:
- 对RValue对象调用的LValue ref限定成员函数
- 检查哪个对象调用了另一个对象的对象方法
- 在 C++ 的 Switch Case 中创建对象后对对象调用方法
- 从 Base 引用对象调用派生类的成员
- 为什么为未删除的对象调用析构函数?
- Qt c++不会为所有对象调用move_slot.为什么?
- 使用在堆栈上创建的对象调用虚拟函数
- 使用基类对象调用Dervied Class函数
- C++:允许临时对象调用非常量成员函数的设计理念是什么?
- 从类中的对象调用类中的函数的最佳方法
- 派生对象调用的 Base 方法的模板推导
- 如何使用单个对象调用具有相同名称的两个类函数
- 是否可以从另一个类对象调用一个类函数而不继承第一个类
- 如果类没有任何成员变量,则通过临时对象调用类的成员函数的开销是多少?
- 如何对动态数组中的某些对象调用析构函数
- 如何从列表中存储的对象调用成员函数
- 从线程内的对象调用静态方法
- 从成员对象调用方法
- 当包含它的对象调用其析构函数时,unique_ptr是否未分配
- 从对象调用成员对象,错误:引用非常量值的初始值必须是左值