//Here's a class that holds the first array:
class test
{
private:
// some other members
public:
int (&array1)[3];
test(int (*newvals)[3]) : array1(*newvals) {}
// some more members
};
void testproblem()
{
int largearray[5];
for (int t = 0; t < 5; t++) largearray[t] = 0;
// create an object named 'testclass' of type test, with largearray elements 1, 3, 4
// If I now change largearray[4] to 3,
// (largearray[4] == testclass.array[2]) should return 1
}
I've been trying to get around this for a few days now - any suggestions?
Thanks,
-Medgur
Edited by - Medgur on 7/17/00 3:10:14 PM
Arrays, references, and pointers
This is a repost, but the original question seems to have been buried, and was poorly worded. (read:confusing?)
I have a large array of integers. I have another class that holds references to 3 elements of an integer array. I want to be able to pass 3 elements of the large array to an object of the class' type, and be able to have changes in the large array be reflected in the appropriate elements of class' array.
To make it simpler:
#include <stdio.h>int main(){ int largearray[10]; int *smallarray[3]; for (int i = 0; i < 3; i++) { smallarray<i> = &largearray[i]; } for (i = 0; i < 10; i++) { largearray[i] = 0; } largearray[2] = 10; printf("largearray[2] = %d\n", largearray[2]); printf("smallarray[2] = %d", *smallarray[2]); return 0;}
Is this what you wanted?!?!
-------------------------------------
That's just my 200 bucks!
..-=gLaDiAtOr=-..
Edited by - Gladiator on July 17, 2000 3:40:48 PM
Thanks, but not quite, integrating it with what I was trying to perform, I get this:
But "ANSI C++ forbids comparison between pointer and integer"
Thanks,
-Medgur
Edited by - Medgur on July 17, 2000 4:07:42 PM
Edited by - Medgur on July 17, 2000 4:10:28 PM
Edited by - Medgur on July 17, 2000 4:11:17 PM
class test{ public: int (&array1)[3]; test(int (*newvals)[3]) : array1(*newvals) {}};void testproblem(){ int largearray[5]; int (*temparray)[3]; for (int t = 0; t < 5; t++) largearray[t] = 0; temparray<0> = &largearray[1]; temparray<1> = &largearray[3]; temparray<2> = &largearray[4]; tclass = new test(temparray); if (tclass.array1[0] == largearray[1]) cout << "It worked"; else cout << "It didn't work.";}
But "ANSI C++ forbids comparison between pointer and integer"
Thanks,
-Medgur
Edited by - Medgur on July 17, 2000 4:07:42 PM
Edited by - Medgur on July 17, 2000 4:10:28 PM
Edited by - Medgur on July 17, 2000 4:11:17 PM
Hrm. Doesn't seem that ANSI C++ allows for this sort of thing to take place, thanks anyways.
-Medgur
Edited by - Medgur on July 18, 2000 1:17:26 PM
-Medgur
Edited by - Medgur on July 18, 2000 1:17:26 PM
You''re declaring function pointers. Is that what you reall want to accomplish? Or are you supposed to work with integer pointers? Not sure what you''re trying to do.
-------------------------------------
That's just my 200 bucks!
..-=gLaDiAtOr=-..
-------------------------------------
That's just my 200 bucks!
..-=gLaDiAtOr=-..
July 19, 2000 07:50 PM
Why would you want to compare a pointer and an integer?
You do realise that an array is actually a pointer to the first element? The [] operator simply offsets from that first element.
What are you using <> for ????
What are you trying to do? It doesn''t make sense ?!
You do realise that an array is actually a pointer to the first element? The [] operator simply offsets from that first element.
What are you using <> for ????
What are you trying to do? It doesn''t make sense ?!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement