C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 9th, 2006, 11:21 AM
riddler2k84 riddler2k84 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2006
Posts: 6 riddler2k84 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #2  
Old November 9th, 2006, 11:43 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,840 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 19 h 25 m 12 sec
Reputation Power: 1774
Like
Code:
class classB {
  classA *anArrayOfThem;
}

Or have you already tried that (or something similar), and have some error messages to share with us?
Comments on this post
mvantuyl agrees!
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
  #3  
Old November 10th, 2006, 07:32 AM
riddler2k84 riddler2k84 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2006
Posts: 6 riddler2k84 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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...

Reply With Quote
  #4  
Old November 10th, 2006, 09:48 AM
Lux Perpetua Lux Perpetua is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,936 Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 h 12 m 42 sec
Reputation Power: 1312
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?

Reply With Quote
  #5  
Old November 10th, 2006, 11:06 AM
riddler2k84 riddler2k84 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2006
Posts: 6 riddler2k84 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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..

Reply With Quote
  #6  
Old November 10th, 2006, 02:21 PM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,840 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 19 h 25 m 12 sec
Reputation Power: 1774
> 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.

Reply With Quote
  #7  
Old November 11th, 2006, 07:29 AM
riddler2k84 riddler2k84 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2006
Posts: 6 riddler2k84 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #8  
Old November 11th, 2006, 09:49 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,840 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 19 h 25 m 12 sec
Reputation Power: 1774
> 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 ]
Comments on this post
riddler2k84 agrees!

Reply With Quote
  #9  
Old November 12th, 2006, 09:06 AM
riddler2k84 riddler2k84 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2006
Posts: 6 riddler2k84 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Pointers pointing to array of objects

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap