简体   繁体   English

如何通过指针数组访问类的成员函数?

[英]How can I access a class's member function via an array of pointers?

I have a pretty standard class with some public member functions and private variables. 我有一个漂亮的标准类,其中包含一些公共成员函数和私有变量。

My problem originally stems from not being able to dynamically name object instances of my class so I created an array of pointers of the class type: 我的问题最初源于无法动态命名类的对象实例,因此我创建了一个类类型的指针数组:

static CShape* shapeDB[dbSize];

I have some prompts to get info for the fields to be passed to the constructor (this seems to work): 我有一些提示来获取要传递给构造函数的字段的信息(这似乎起作用):

shapeDB[CShape::openSlot] = new CShape(iParam1,sParam1,sParam2);

openSlot increments properly so if I were to create another CShape object, it would have the next pointer pointing to it. openSlot会适当增加,因此如果我要创建另一个CShape对象,它将有下一个指向它的指针。 This next bit of code doesn't work and crashes consistently: 接下来的代码不起作用,并且始终崩溃:

cout << shapeDB[2]->getName() << " has a surface area of: " << shapeDB[2]->getSA() << shapeDB[2]->getUnits() << endl;

The array of pointers is declared globally outside of main and the get() functions are public within the class returning strings or integers. 指针数组在main之外全局声明,并且get()函数在返回字符串或整数的类中是公共的。 I'm not sure what I'm doing wrong but something relating to the pointer set up I'm sure. 我不确定自己在做什么错,但是我确定与指针设置有关。 I'm writing this code to try and learn more about classes/pointers and have gotten seriously stumped as I can't find anyone else trying to do this. 我正在编写此代码,以尝试学习有关类/指针的更多信息,并且由于找不到其他尝试这样做的人而陷入了困境。

I'm also curious as to what the CShape new instances get named..? 我也对CShape新实例的命名感到好奇。 if there is any other way to dynamically create object instances and track the names so as to be able to access them for member functions, I'm all ears. 如果还有其他方法可以动态创建对象实例并跟踪名称,以便能够为成员函数访问它们,那么我非常高兴。

I've tried all sorts of permutations of pointer referencing/de-referencing but most are unable to compile. 我尝试了各种指针引用/解引用的排列方式,但是大多数都无法编译。 I can post larger chunks or all of the code if anyone thinks that will help. 如果有人认为有帮助,我可以发布更大的代码块或所有代码。

class CShape {
    int dim[maxFaces];
    int faces;
    string units;
    string type;
    string name;
    bool initialized;
    int slot;
public:
    static int openSlot;

    CShape();
    CShape(int, string, string); // faces, units, name
    ~CShape();

    void initialize(void);

    // external assist functions
    int getA(void) {
        return 0;
    }
    int getSA(void) {
        int tempSA = 0;

        // initialize if not
        if(initialized == false) {
            initialize();
        }

        // if initialized, calculate SA
        if(initialized == true) {
            for(int i = 0; i < faces; i++)
            {
                tempSA += dim[i];
            }
            return(tempSA);
        }

        return 0;
    }
    string getUnits(void) {
        return(units);
    }
    string getName(void) {
        return(name);
    }

    // friend functions
    friend int printDetails(string);
};

// constructor with values
CShape::CShape(int f, string u, string n) {

    initialized = false;
    faces = f;
    units = u;
    name = n;
    slot = openSlot;
    openSlot++;
}

My guess is you use the CShape constructor to increment CShape::openSlot? 我的猜测是您使用CShape构造函数来递增CShape :: openSlot吗? You're probably changing the value before it's read, thus the pointer is stored in a different location. 您可能在读取值之前就更改了它,因此指针存储在其他位置。

Try replacing openSlot with a fixed value to rule out this CShape::option. 尝试用固定值替换openSlot以排除此CShape :: option。

-- code was added -- -添加了代码-

I'm pretty sure this is the problem, the constructor is executed before the asignment, which means the lhs. 我很确定这是问题所在,构造函数是在赋值之前执行的,这意味着lhs。 will be evaluated after CShape::openSlot is incremented. 将在CShape :: openSlot增加之后求值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过指针从函数访问类的成员? - How can I access a member of a class from a function via pointer? 我如何获得一个类成员函数来访问另一个类成员函数的私有成员? - How do i get a class member function to have access to another class member function's private member? 如何通过共享 ptr 到共享 ptr 访问类的成员函数? - How can I access member function of a class via a shared ptr to a shared ptr? Function 指向类成员 function - Function pointers on a class's member function 如何使用指向类成员函数的函数指针简化C ++函数? - How can I simplify C++ functions with function pointers to class member functions? 如何创建与不同对象耦合的成员 function 指针数组? - How can I create an array of member function pointers coupled with different objects? 如何通过包装类访问direcly类成员的方法 - how to access direcly class member's methods via wrapper class 为什么我可以通过指向派生对象的基类指针访问派生的私有成员函数? - Why can I access a derived private member function via a base class pointer to a derived object? 如何通过指针的类指针访问类的成员函数? - How can i access member functions of a class via Class pointer of pointer? 如何使用 function 指针访问成员函数? - How to use function pointers to access member functions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM