如何从列表中存储的对象调用成员函数

How to call member functions from objects stored in a list

本文关键字:对象 调用 成员 函数 存储 列表      更新时间:2023-10-16

我在列表中存储了几个对象。如何调用列表中对象的成员函数?

我需要访问列表中存储的每个对象,并调用每个对象以对每个对象进行分类。如果何时要做这件事要比比较每个对象的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());});