The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Pointers pointing to array of objects
Discuss Pointers pointing to array of objects in the C Programming forum on Dev Shed. Pointers pointing to array of objects C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 9th, 2006, 11:21 AM
|
|
Registered User
|
|
Join Date: Nov 2006
Posts: 6
Time spent in forums: 1 h 49 m 28 sec
Reputation Power: 0
|
|
|
Pointers pointing to array of objects
hey people, this is my first post, I have a problem that stumped me, this is the scenario:
I have 2 classes, class A and class B. Class A contains all the variables, getters, and setters. In class B, I need to create a pointer that points to an array of Object of class A.
Can some one help me in this?
|

November 9th, 2006, 11:43 AM
|
 |
Contributed User
|
|
|
|
Like
Code:
class classB {
classA *anArrayOfThem;
}
Or have you already tried that (or something similar), and have some error messages to share with us?
|

November 10th, 2006, 07:32 AM
|
|
Registered User
|
|
Join Date: Nov 2006
Posts: 6
Time spent in forums: 1 h 49 m 28 sec
Reputation Power: 0
|
|
|
class A{
public:
int a;
A(){
a=0;
}
int getA()
{
return a;
}
void setA(int z)
{
a=z;
}
};
class B{
public:
A *ptr;
A *array[3];
static int count;
B ()
{
contruc();
*ptr= array[];
count=0;
}
void contruc ()
{
for (int i=0; i<=2; i++)
array[i]= new A();
}
void insert()
{
}
void show()
{
}
};
void main ()
{
B b1;
}
there is an error which I can't manage to figure out...
|

November 10th, 2006, 09:48 AM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: San Francisco Bay
|
|
|
Please use code tags to post code ([code]Your code here[/code]). Also, it's better to state the problem explicitly than to say "there is a problem" and expect us to find it.
I can't tell what the intended purposes of the variables ptr, array, and the function contruc() are, or what you mean by the line *ptr = array[];. Could you explain it in words?
|

November 10th, 2006, 11:06 AM
|
|
Registered User
|
|
Join Date: Nov 2006
Posts: 6
Time spent in forums: 1 h 49 m 28 sec
Reputation Power: 0
|
|
sorry about that... let me be a little bit more specific:
First I have a class:
Code:
class A{
int numA;
A()
{
numA=0;
}
void set(int numZ)
{
numA=numZ;
}
int get()
{
return numA;
}
};
Now the problem is that I have to create another class call class B which contains a pointer, lets call it ptr, which is used to point to an array of A object, and a integer counter, which is used to store the number of existing As currently. I need to have a constructor to create an array for the pointer.
Now normally it is not too difficult in Java, but I am not strong in C... so any help is appreciated..
|

November 10th, 2006, 02:21 PM
|
 |
Contributed User
|
|
|
|
|
> lets call it ptr, which is used to point to an array of A object,
Given
A *ptr;
A *array[3];
A valid assignment would be
ptr = array[0];
But since you only did
array[ i ]= new A();
ptr is only pointing at ONE A
Now if you did
array[ i ]= new A[10]; // create 10 of them,
Then
ptr = array[0];
would indeed be a pointer to an array of A's
Remember that array itself is an array of pointers to A.
> void main ()
main returns an int.
|

November 11th, 2006, 07:29 AM
|
|
Registered User
|
|
Join Date: Nov 2006
Posts: 6
Time spent in forums: 1 h 49 m 28 sec
Reputation Power: 0
|
|
Thanks for the reply...
Quote: | Originally Posted by salem >
Now if you did
array[ i ]= new A[10]; // create 10 of them,
|
now this is weird, I am possitively sure that I should use:
Code:
array[i] = new A();
since this would in fact create and array of A objects of size i. I will try out your suggestion anyway
Quote: | Originally Posted by salem >
Then
ptr = array[0];
would indeed be a pointer to an array of A's
Remember that array itself is an array of pointers to A.
|
I need the pointer to point the the array, this method seems only to point to the first object in the array: array[0]; How would I then access to the other objects in the array?
|

November 11th, 2006, 09:49 AM
|
 |
Contributed User
|
|
|
|
|
> since this would in fact create and array of A objects of size i.
But i is on the left of the expression, not the right.
If you have
array[ i ]= new A[10]; // create 10 of them,
ptr = array[0];
Then ptr[0] is array[0][0] and ptr[9] is array[0][9]
In other words, ptr is just an alias for what you've stored in array[ i ]
|

November 12th, 2006, 09:06 AM
|
|
Registered User
|
|
Join Date: Nov 2006
Posts: 6
Time spent in forums: 1 h 49 m 28 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by salem > since this would in fact create and array of A objects of size i.
But i is on the left of the expression, not the right.
If you have
array[ i ]= new A[10]; // create 10 of them,
ptr = array[0];
Then ptr[0] is array[0][0] and ptr[9] is array[0][9]
In other words, ptr is just an alias for what you've stored in array[ i ] |
Ah... I get it now... I am new to C++, thanks for your help... many thanks 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|