C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old December 15th, 2004, 01:51 PM
lawngnomes69 lawngnomes69 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 1 lawngnomes69 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Help with class constructors?

Alright here is the problem. I am able to pass variables into a class with constructors but am having dificulty in passing arrays. The array already exists inside of the class but i want to give it values when I create a variable of my class. Now I know that i need to use pointers to pass arrays into a function like so...

int add (int *list);

main(){
int list[5]={1,2,3,4,5};
cout << add(list);
}

int add (int *list){
int x;
x=list[0]+list[1]+list[2]+list[3]+list[4];
return x;
}

Now while this works fine if i'd try and do something in a constructor like this...

class acsl{
private:
int card[5];
int check[5];
int combos[13];
int flush[5];
int two;
int three;
int four;
int straight;
short j;

public:
void reset();
void adjust();
void print();
void input();
acsl(int *card);

};
acsl::acsl(int *card)
{
acsl::card=card;
}

Where a list of cards is what i want to assign when i create a variable of my class.

Reply With Quote
  #2  
Old March 26th, 2005, 01:36 PM
ubergeek ubergeek is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2005
Posts: 600 ubergeek User rank is Private First Class (20 - 50 Reputation Level)ubergeek User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 4
Send a message via AIM to ubergeek
to access objects of a class from within a member function, use the this pointer.

Code:
this->card = card;


the this pointer is created and destroyed automatically by the compiler, and points to the instance of the class that the member function was called for.


FYI, you don't actually need to use the this pointer to access member variables from within member functions, so this code
Code:
card=card;

would work the same as my first example.

Last edited by ubergeek : March 26th, 2005 at 01:37 PM. Reason: added semicolons (d'oh!)

Reply With Quote
  #3  
Old March 29th, 2005, 11:00 AM
BoolBooB BoolBooB is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 36 BoolBooB User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 35 m 42 sec
Reputation Power: 4
Code:
 
acsl::acsl(int *card)
{
acsl::card=card;
}

Wont work. You can't simply assign arrays you must copy them. Try this:
Code:
 
acsl::acsl(int *inCard)
{
for (int i = 0; i <= 4; i++) {
card[i] = inCard[i]
}
}

Also, get away from using hard coded values for your array sizes - use consts instaed. That way if you ever want to change the size of the array, you just change the const and you don't have to go through your code looking for all the places you hard coded the array size. Makes software maintenace a whole lot easier.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Help with class constructors?


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 | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway