using pointers

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

Join Date: Jun 2007
Posts: 20
Reputation: vladdy19 is an unknown quantity at this point 
Solved Threads: 0
vladdy19's Avatar
vladdy19 vladdy19 is offline Offline
Newbie Poster

using pointers

 
0
  #1
Jun 27th, 2007
I have a Class Personnel and a sub-class Student
how could i change a pointer pointing to Personnel and have it point to Student?

I'm trying this

  1. Personnel *person=NULL;
  2. PersonnelNode *node=NULL;
  3. PersonnelNode *temp, *prev;
  4. Student *s=new Student();
  5. while (temp != NULL) {
  6. person = temp->getNode();
  7. if (stricmp(name,name)==0){
  8. prev = temp;
  9. temp = temp->getNext();
  10. index++;
  11. }
  12. else{
  13. cout <<"Person to delete is found at index" << index<<endl;
  14. display_node(person, index);
  15. s=person;
  16. s->bookQueue->enqueue(title, due);
  17. }
  18.  
  19. }
but it says as an error

error C2440: '=' : cannot convert from 'Personnel *' to 'Student *'
Last edited by WolfPack; Jun 27th, 2007 at 5:56 am. Reason: Added [CODE][/CODE] tags. Learn to use them yourself next time.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,578
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: 1486
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: using pointers

 
0
  #2
Jun 27th, 2007
Objects of type Personnel can not be converted to PersonnelNode because it knows nothing about the data and methods of the child class. Its a matter of inherentence -- PersonnelNode inherits from Personnel, not the other way around.
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: Jun 2007
Posts: 322
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: using pointers

 
0
  #3
Jun 27th, 2007
how could i change a pointer pointing to Personnel and have it point to Student?
You can typecast it.
  1. s = (Student*)person;
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: using pointers

 
0
  #4
Jun 27th, 2007
A pointer pointing to base class can be converted to a pointer pointing to derived class (down-casting) if the original pointer is actually pointing to a derived class type.
To enforce this is the work of dynamic_cast operator.

Consider following example.
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class base
  6. {
  7. public:
  8. virtual ~base() {cout << "Inside ~base()" << endl ;}
  9. virtual void some_function() {cout << "Inside base::some_function()" << endl ;}
  10. };
  11.  
  12. class derived : public base
  13. {
  14. public:
  15. ~derived() {cout << "Inside ~derived()" << endl ;}
  16. void some_function() {cout << "Inside derived::some_function()" << endl ;}
  17. void special_function() {cout << "Inside derived::special_function()" << endl ;}
  18. };
  19.  
  20. void free_function( base *pb )
  21. {
  22. pb->some_function() ;
  23. //special case, when we need to do something with derived class..
  24. //example of bad OOD. :)
  25. derived *converted_pd = dynamic_cast< derived* > ( pb ) ;
  26. if( 0 != converted_pd )
  27. converted_pd->special_function() ;
  28. else
  29. cout << "dynamic_cast returned NULL. pb was NOT pointing to an obj of type derived." << endl ;
  30. }
  31.  
  32. int main()
  33. {
  34. derived *pd = new derived() ;
  35. base *pb = new base() ;
  36. base *converted_pb = dynamic_cast<base*>( pd ) ;
  37.  
  38. cout << "Calling free_function() with converted_pb." << endl ;
  39. free_function(converted_pb) ;
  40.  
  41. cout << "Calling free_function() with pb." << endl ;
  42. free_function(pb) ;
  43.  
  44. return 0 ;
  45. }
Here is my output:
Calling free_function() with converted_pb.
Inside derived::some_function()
Inside derived::special_function()
Calling free_function() with pb.
Inside base::some_function()
dynamic_cast returned NULL. pb was NOT pointing to an obj of type derived.

Press ENTER to continue.

Originally Posted by Ancient Dragon View Post
Objects of type Personnel can not be converted to PersonnelNode
I think it's a typo?? If not it's correct but irrelevant.
Last edited by thekashyap; Jun 27th, 2007 at 9:50 am.
Are you Agile.. ?
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: using pointers

 
0
  #5
Jun 27th, 2007
Originally Posted by Hamrick View Post
You can typecast it.
  1. s = (Student*)person;
One shouldn't do this in C++. Use C++ style casting instead (of C-style)
Are you Agile.. ?
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 322
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: using pointers

 
0
  #6
Jun 27th, 2007
Use C++ style casting instead (of C-style)
Why? They do the same thing. And C++ typecasts are too long.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: using pointers

 
0
  #7
Jun 27th, 2007
Originally Posted by Hamrick View Post
Why? They do the same thing. And C++ typecasts are too long.
In very gentle words NO.
Output of this code:
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class base
  6. {
  7. public:
  8. virtual ~base() {cout << "Inside ~base()" << endl ;}
  9. virtual void some_function() {cout << "Inside base::some_function()" << endl ;}
  10. };
  11.  
  12. class derived : public base
  13. {
  14. public:
  15. ~derived() {cout << "Inside ~derived()" << endl ;}
  16. void some_function() {cout << "Inside derived::some_function()" << endl ;}
  17. void special_function() {cout << "Inside derived::special_function()" << endl ;}
  18. };
  19.  
  20. void free_function( base *pb )
  21. {
  22. pb->some_function() ;
  23. //special case, when we need to do something with derived class..
  24. //example of bad OOD. :)
  25. derived *converted_pd = dynamic_cast< derived* > ( pb ) ;
  26. if( 0 != converted_pd )
  27. converted_pd->special_function() ;
  28. else
  29. cout << "dynamic_cast returned NULL. pb was NOT pointing to an obj of type derived." << endl ;
  30.  
  31. cout << endl << "Doing C-style casting" << endl ;
  32. converted_pd = (derived*) pb ;
  33. if( 0 != converted_pd )
  34. converted_pd->special_function() ;
  35. else
  36. cout << "C-style cast returned NULL. pb was NOT pointing to an obj of type derived." << endl ;
  37. }
  38.  
  39. int main()
  40. {
  41. derived *pd = new derived() ;
  42. base *pb = new base() ;
  43. base *converted_pb = dynamic_cast<base*>( pd ) ;
  44.  
  45. cout << endl << "Calling free_function() with converted_pb." << endl ;
  46. free_function(converted_pb) ;
  47.  
  48. cout << endl << "Calling free_function() with pb." << endl ;
  49. free_function(pb) ;
  50.  
  51. return 0 ;
  52. }
is this:
-----------
Calling free_function() with converted_pb.
Inside derived::some_function()
Inside derived::special_function()

Doing C-style casting
Inside derived::special_function()

Calling free_function() with pb.
Inside base::some_function()
dynamic_cast returned NULL. pb was NOT pointing to an obj of type derived.

Doing C-style casting
Inside derived::special_function()

Press ENTER to continue.
-----------
There is your difference and I'm sure you understand the problems associated with it. If not I will explain.
Last edited by thekashyap; Jun 27th, 2007 at 10:08 am. Reason: fixed closing code tag
Are you Agile.. ?
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 322
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: using pointers

 
0
  #8
Jun 27th, 2007
If you know that the conversion works, it doesn't matter, right? If you don't know, you shouldn't be trying to typecast anyway.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: using pointers

 
1
  #9
Jun 28th, 2007
Originally Posted by Hamrick View Post
If you know that the conversion works, it doesn't matter, right?
Now that's perfect question to be answered with someone's signature (I really searched for it and couldn't find) which goes something like:
"Although it might be possible to collect the twigs using your nose, it doesn't necessarily mean it's the best way."
I'm assuming you didn't see the problem.
------------------------------------------
Calling free_function() with pb. => Remember pb points to object of type base, which does not implement a function named special_function().

Inside base::some_function()
dynamic_cast returned NULL. pb was NOT pointing to an obj of type derived. => dynamic_cast knows that pb can not and must not be converted to derived*, so it returns NULL.

Doing C-style casting
Inside derived::special_function() => Even though this object does not have this function a call to it is successful. This is only bad-luck.
------------------------------------------
Now if you still doesn't see the problem, let me give you another one:
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. enum ActionEnum { GET_LAN_STATUS,
  6. SHUTDOWN_LAN
  7. } ;
  8.  
  9. class employee
  10. {
  11. public:
  12. employee(const string nameStr):name(nameStr) {}
  13. virtual ~employee() {cout << "Inside ~employee()" << endl ;}
  14. virtual void get_status_of_office_LAN() {cout << "get_status_of_office_LAN() by: " << name.c_str() << endl ;}
  15. string getname() { return name ; }
  16. protected:
  17. string name ;
  18. };
  19.  
  20. class administrator : public employee
  21. {
  22. public:
  23. administrator(const string nameStr):employee(nameStr) {}
  24. ~administrator() {cout << "Inside ~administrator()" << endl ;}
  25. void get_status_of_office_LAN() {cout << "get_status_of_office_LAN() by: " << name.c_str() << endl ;}
  26. void shutdown_office_LAN() {cout << "LAN shutdown by: " << name.c_str() << endl ;}
  27. };
  28.  
  29. void perform_action( employee *pb, ActionEnum action )
  30. {
  31. switch( action )
  32. {
  33. case GET_LAN_STATUS:
  34. {
  35. pb->get_status_of_office_LAN() ;
  36. break ;
  37. }
  38.  
  39. case SHUTDOWN_LAN:
  40. {
  41. administrator *converted_pd = dynamic_cast< administrator* > ( pb ) ;
  42. if( 0 != converted_pd )
  43. converted_pd->shutdown_office_LAN() ;
  44. else
  45. cout << pb->getname().c_str() << ": does not have required rights to shutdown LAN." << endl ;
  46.  
  47. cout << endl << "Doing C-style casting" << endl ;
  48. converted_pd = (administrator*) pb ;
  49. if( 0 != converted_pd )
  50. converted_pd->shutdown_office_LAN() ;
  51. else
  52. cout << pb->getname().c_str() << ": does not have required rights to shutdown LAN." << endl ;
  53. }
  54. default: break;
  55. }
  56. }
  57.  
  58. int main()
  59. {
  60. administrator *pd = new administrator("Some administrator") ;
  61. employee *pb = new employee("NOT an administrator") ;
  62.  
  63. cout << endl << "Calling perform_action(GET_LAN_STATUS) with pb." << endl ;
  64. perform_action(pb, GET_LAN_STATUS) ;
  65. cout << endl << "Calling perform_action(SHUTDOWN_LAN) with pb." << endl ;
  66. perform_action(pb, SHUTDOWN_LAN) ;
  67.  
  68. return 0 ;
  69. }
and here is the output:
----------------------------------------------------------
Calling perform_action(GET_LAN_STATUS) with pb.
get_status_of_office_LAN() by: NOT an administrator

Calling perform_action(SHUTDOWN_LAN) with pb.
NOT an administrator: does not have required rights to shutdown LAN.

Doing C-style casting
LAN shutdown by: NOT an administrator

Press ENTER to continue.
----------------------------------------------------------
Are you Agile.. ?
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 322
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: using pointers

 
0
  #10
Jun 28th, 2007
Oh. Okay.
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



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC