943,697 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1381
  • C++ RSS
Dec 2nd, 2007
0

pointers and classes

Expand Post »
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...

C++ Syntax (Toggle Plain Text)
  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...

C++ Syntax (Toggle Plain Text)
  1. emp.display();

if this is possible how would i do it?

Thanks for the help
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
cl3m0ns is offline Offline
30 posts
since Oct 2007
Dec 2nd, 2007
0

Re: pointers and classes

Well yes its possible, but why would you want to do it? Unless, of course you are passing a pointer to another function.
C++ Syntax (Toggle Plain Text)
  1. Employee emp;
  2. Employee* ptr = &emp;
  3. ptr->display();
Last edited by Ancient Dragon; Dec 2nd, 2007 at 8:55 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,950 posts
since Aug 2005
Dec 2nd, 2007
0

Re: pointers and classes

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
cl3m0ns is offline Offline
30 posts
since Oct 2007
Dec 2nd, 2007
0

Re: pointers and classes

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,950 posts
since Aug 2005
Dec 2nd, 2007
0

Re: pointers and classes

Click to Expand / Collapse  Quote originally posted by cl3m0ns ...
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.
Moderator
Reputation Points: 3278
Solved Threads: 890
Posting Sage
WaltP is online now Online
7,718 posts
since May 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: c++ letter coding
Next Thread in C++ Forum Timeline: Just a lil fun





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC