| | |
using pointers
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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
but it says as an error
error C2440: '=' : cannot convert from 'Personnel *' to 'Student *'
how could i change a pointer pointing to Personnel and have it point to Student?
I'm trying this
cpp Syntax (Toggle Plain Text)
Personnel *person=NULL; PersonnelNode *node=NULL; PersonnelNode *temp, *prev; Student *s=new Student(); while (temp != NULL) { person = temp->getNode(); if (stricmp(name,name)==0){ prev = temp; temp = temp->getNext(); index++; } else{ cout <<"Person to delete is found at index" << index<<endl; display_node(person, index); s=person; s->bookQueue->enqueue(title, due); } }
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.
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.
•
•
•
•
how could i change a pointer pointing to Personnel and have it point to Student?
C++ Syntax (Toggle Plain Text)
s = (Student*)person;
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.
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.
I think it's a typo?? If not it's correct but irrelevant.
To enforce this is the work of dynamic_cast operator.
Consider following example.
cpp Syntax (Toggle Plain Text)
#include <iostream> using namespace std; class base { public: virtual ~base() {cout << "Inside ~base()" << endl ;} virtual void some_function() {cout << "Inside base::some_function()" << endl ;} }; class derived : public base { public: ~derived() {cout << "Inside ~derived()" << endl ;} void some_function() {cout << "Inside derived::some_function()" << endl ;} void special_function() {cout << "Inside derived::special_function()" << endl ;} }; void free_function( base *pb ) { pb->some_function() ; //special case, when we need to do something with derived class.. //example of bad OOD. :) derived *converted_pd = dynamic_cast< derived* > ( pb ) ; if( 0 != converted_pd ) converted_pd->special_function() ; else cout << "dynamic_cast returned NULL. pb was NOT pointing to an obj of type derived." << endl ; } int main() { derived *pd = new derived() ; base *pb = new base() ; base *converted_pb = dynamic_cast<base*>( pd ) ; cout << "Calling free_function() with converted_pb." << endl ; free_function(converted_pb) ; cout << "Calling free_function() with pb." << endl ; free_function(pb) ; return 0 ; }
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.
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.. ?
One shouldn't do this in C++. Use C++ style casting instead (of C-style)
Are you Agile.. ?
In very gentle words NO.
Output of this code:
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.
Output of this code:
cpp Syntax (Toggle Plain Text)
#include <iostream> using namespace std; class base { public: virtual ~base() {cout << "Inside ~base()" << endl ;} virtual void some_function() {cout << "Inside base::some_function()" << endl ;} }; class derived : public base { public: ~derived() {cout << "Inside ~derived()" << endl ;} void some_function() {cout << "Inside derived::some_function()" << endl ;} void special_function() {cout << "Inside derived::special_function()" << endl ;} }; void free_function( base *pb ) { pb->some_function() ; //special case, when we need to do something with derived class.. //example of bad OOD. :) derived *converted_pd = dynamic_cast< derived* > ( pb ) ; if( 0 != converted_pd ) converted_pd->special_function() ; else cout << "dynamic_cast returned NULL. pb was NOT pointing to an obj of type derived." << endl ; cout << endl << "Doing C-style casting" << endl ; converted_pd = (derived*) pb ; if( 0 != converted_pd ) converted_pd->special_function() ; else cout << "C-style cast returned NULL. pb was NOT pointing to an obj of type derived." << endl ; } int main() { derived *pd = new derived() ; base *pb = new base() ; base *converted_pb = dynamic_cast<base*>( pd ) ; cout << endl << "Calling free_function() with converted_pb." << endl ; free_function(converted_pb) ; cout << endl << "Calling free_function() with pb." << endl ; free_function(pb) ; return 0 ; }
-----------
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.. ?
•
•
•
•
If you know that the conversion works, it doesn't matter, right?
"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:
cpp Syntax (Toggle Plain Text)
#include <iostream> using namespace std; enum ActionEnum { GET_LAN_STATUS, SHUTDOWN_LAN } ; class employee { public: employee(const string nameStr):name(nameStr) {} virtual ~employee() {cout << "Inside ~employee()" << endl ;} virtual void get_status_of_office_LAN() {cout << "get_status_of_office_LAN() by: " << name.c_str() << endl ;} string getname() { return name ; } protected: string name ; }; class administrator : public employee { public: administrator(const string nameStr):employee(nameStr) {} ~administrator() {cout << "Inside ~administrator()" << endl ;} void get_status_of_office_LAN() {cout << "get_status_of_office_LAN() by: " << name.c_str() << endl ;} void shutdown_office_LAN() {cout << "LAN shutdown by: " << name.c_str() << endl ;} }; void perform_action( employee *pb, ActionEnum action ) { switch( action ) { case GET_LAN_STATUS: { pb->get_status_of_office_LAN() ; break ; } case SHUTDOWN_LAN: { administrator *converted_pd = dynamic_cast< administrator* > ( pb ) ; if( 0 != converted_pd ) converted_pd->shutdown_office_LAN() ; else cout << pb->getname().c_str() << ": does not have required rights to shutdown LAN." << endl ; cout << endl << "Doing C-style casting" << endl ; converted_pd = (administrator*) pb ; if( 0 != converted_pd ) converted_pd->shutdown_office_LAN() ; else cout << pb->getname().c_str() << ": does not have required rights to shutdown LAN." << endl ; } default: break; } } int main() { administrator *pd = new administrator("Some administrator") ; employee *pb = new employee("NOT an administrator") ; cout << endl << "Calling perform_action(GET_LAN_STATUS) with pb." << endl ; perform_action(pb, GET_LAN_STATUS) ; cout << endl << "Calling perform_action(SHUTDOWN_LAN) with pb." << endl ; perform_action(pb, SHUTDOWN_LAN) ; return 0 ; }
----------------------------------------------------------
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.. ?
![]() |
Similar Threads
- Pointers (archived tutorial) (C++)
- Problem About Pointers (C++)
- New to C++ Pointers and need help with identifying where the errors are (C++)
- Linked List using pointers (C++ ADT) (C++)
- Why can't I use Pointers to point to a Enumurated Constant (C++)
Other Threads in the C++ Forum
- Previous Thread: How do I initialize an array from infile?
- Next Thread: Help me plz, LIST CONTROL
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library linker list loop looping loops map math matrix memory microsoft newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings struct temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






