pointers and classes

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2007
Posts: 30
Reputation: cl3m0ns is an unknown quantity at this point 
Solved Threads: 0
cl3m0ns's Avatar
cl3m0ns cl3m0ns is offline Offline
Light Poster

pointers and classes

 
0
  #1
Dec 2nd, 2007
i have a class called Employee and a method in that class called display(). When that method is called it displays a first name and a last name.

In my int main() i have...

  1. int main()
  2. {
  3. Employee emp;
  4. Employee* ptr;
  5.  
  6. return 0 ;
  7. }

I was wondering if there were a way to have the pointer point to emp and then use the pointer to display emp as opposed to simply saying...

  1. emp.display();

if this is possible how would i do it?

Thanks for the help
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1461
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: pointers and classes

 
0
  #2
Dec 2nd, 2007
Well yes its possible, but why would you want to do it? Unless, of course you are passing a pointer to another function.
  1. Employee emp;
  2. Employee* ptr = &emp;
  3. ptr->display();
Last edited by Ancient Dragon; Dec 2nd, 2007 at 8:55 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 30
Reputation: cl3m0ns is an unknown quantity at this point 
Solved Threads: 0
cl3m0ns's Avatar
cl3m0ns cl3m0ns is offline Offline
Light Poster

Re: pointers and classes

 
0
  #3
Dec 2nd, 2007
Originally Posted by Ancient Dragon View Post
Well yes its possible, but why would you want to do it?
[/code]
I was just trying to figure out how powerful or useful pointers are because they just seem worthless to me...thanks for your help though.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1461
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: pointers and classes

 
0
  #4
Dec 2nd, 2007
one purpose of pointers to to allocate an array of something because the size is not known until the program is run.
Employee* empArray = new Employee[NumberEmployees];
Another purpose: creating a linked list of Employee classes. Admittedly most c++ programmers would using c++ container classes such as vector or dqueue.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: pointers and classes

 
0
  #5
Dec 2nd, 2007
Originally Posted by cl3m0ns View Post
I was just trying to figure out how powerful or useful pointers are because they just seem worthless to me...thanks for your help though.
Then you simply don't know enough of the language yet. The knowledge and understanding will come. You don't need to rush it yet.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC