Click here to Skip to main content
15,299,255 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
For class A:

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


If in a loop of n iterations I have

C++
*pA = pA[n];


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
--------------------------------------------


C++
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
    
    }
    
}
Posted
Updated 2-Nov-12 6:56am
v4

Your first line seems to be cause of your lack of understanding:
A *pA = A[n]; // pointer to n objects of class A

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900