指向类的指针 [英] pointer to a pointer to a class

查看:78
本文介绍了指向类的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于A类:

For class A:

A *pA = A[n];  // pointer to n objects of class A



如果在n次迭代的循环中,我有



If in a loop of n iterations I have

*pA = pA[n];



创建和更改第一个对象后,如何制作指向该对象的指针(以及将pA循环1到pA [n + 1]?

可能吗?我尝试使用* pPrevObject = pA [n];在循环的末尾,但它没有指向此信息.我的问题是此信息是否保留在该对象中,以及如何声明指向先前创建的对象的指针...

对此过程的任何解释,无论它是否直接回答了我的问题,都将不胜感激.我是C ++编程的新手,但我对将指针与类一起使用的了解还不是很好...

代码:------------------------------------
这不包括#includes等-只是一个概述
--------------------------------------------




How do I make a pointer to this first object after it has been created and n changed (along with pA in 1 loop to pA[n+1]?

Is it possible? I tried to use *pPrevObject = pA[n]; at the end of the loop but it doesnt point to this information. My question is does this info stay in that object, and how do i declare a pointer to the object created previously...

Any explanation of this process whether it directly answers my question or not would be appreciated. I am new to programming in C++ and my understanding of using pointers with classes isn''t great... yet!

code:------------------------------------
This is excluding the #includes etc - just an overview
--------------------------------------------


class A
{
public:
    double Grabdata(int x);
    void Passdata(int x);
    double Output(int x);
    
private:
    vector <double> data;
}

main()
{
    A *pointer = new A[3];
    A *pPrevious;
    
    for(int n=0; n<3; n++)
    {
        *pointer = pointer[n];
        
        for (i=0; i<10; i++) //iterates for all 10 stored elements in data vector
        { 
            double data = pointer->Grabdata;      // this gets data from the vector
            double output = pointer->Output(data);        // this generates the 
                                                    //corresponding output
            pointer->Passdata(output);
        }
        
    // now i want these outputs to be the data for next iteration (ie the 
    // inputs for calculating - in the new object)
    // now this is dumbed down version i know there's no point for this
    // to need to go next instance but in my very large program there is
    
    // i want to somehow point to the first object that has been created
    // with its data inside altered
    // and transfer this data accross for this (now second iteration of the loop)
    // object ie pointer[n+1] instance
    // i hope this makes more sense
    
    }
    
}

推荐答案

您的第一行似乎是您缺乏理解的原因:
Your first line seems to be cause of your lack of understanding:
A *pA = A[n]; // pointer to n objects of class A


在这里,pA不是指向类A的n个对象的指针,而是指向单个对象(即数组A中的第n个对象)的指针.不幸的是,数组名与类名重合,这使得它更加难以看清.在此语句中继续进行.

对于其余的问题:请给出一个代码示例,说明您到底要完成什么.您的一般描述不是很清楚.


Here, pA is not a pointer to n objects of class A, but a pointer to a single object, namely the n-th object in array A. Unfortunately the array name coincides with the class name which makes it even harder to see what''s going on in this statement.

For the rest of your question: Please give a code example of what exactly you are trying to accomplish. Your general description is not very clear.


您的for循环的第一条语句不好.
* pointer =指针[n];
在第二次和第三次迭代之后,指针将丢失其先前的元素,指针[0],指针[1].
您可以创建一个临时指针pointerTemp,以保存A的第n个实例,它不会丢失其他指针实例.
First statement of your for loop is not good.
*pointer = pointer[n];
After second and third iteration, pointer will lost its previous elements, pointer[0],pointer[1].
You can create a temporary pointer pointerTemp, to hold nth instance of A, it will not lost other instances of pointer.
for(int n=0; n<3; n++)
    {
        A* pointerTemp = pointer[n];

        for (int i=0; i<10; i++) //iterates for all 10 stored elements in data vector
        { 
            double data = pointerTemp->Grabdata(i); // this gets data from the vector
            double output = pointerTemp->Output(data); // this generates the 
            //corresponding output
            pointerTemp->Passdata(output);
        }
    }


这篇关于指向类的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆