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

时间:2017-04-29 23:29:05

标签: c++ list function member

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

我需要访问列表中存储的每个对象,并为每个对象调用getX函数来对对象进行排序。如果有更好的时间做这个比比较每个对象的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 << "\n\n///////////////////\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;
}

2 个答案:

答案 0 :(得分:0)

您需要确保成员函数是公共的, 使用迭代器访问列表元素。

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());});

答案 1 :(得分:0)

您需要使用比较器功能根据某些自定义标准对列表进行排序。例如,如果要对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());
});

在这种情况下,您无需制作明确的比较器功能。