dynamic casting while initializing a class list

I'm trying to initialize a list of derived/base class pointers to a base type(Employee) head from a file using an overloaded extraction operator. When i initialize each node i'm checking the jobTitle and using dynamic_cast to convert the current Eptr node to a derived class(Programmer of SoftwareArch). However the program is crashing whenever i trying dynamic_cast.

I'm new to dynamic_cast and am not sure exactly how to utilize it.

I wish to keep all the data members that were initialized before the cast AND then finish initializing the data members relative to Programmer or SoftwareArch.


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
        Employee* employees=NULL;
	Employee* Eptr=NULL;
	Programmer *Pptr=NULL;
	SoftwareArch *SAptr=NULL;

	ifstream ifile;
	ifile.open("inputFile.txt");

	while(ifile)
	{
		Eptr=new Employee();
		ifile >> Eptr;

		if(Eptr->ReturnJobTitle()=="Programmer")
		{

			Eptr = dynamic_cast<Programmer*>(Eptr);
			ifile >> Eptr;

			Eptr->next=employees;
			employees=Eptr;
			Eptr=NULL;
		}
		else if(Eptr->ReturnJobTitle()=="Software Architect")
		{
			Eptr = dynamic_cast<SoftwareArch*>(Eptr);
			ifile >> Eptr;
	
			Eptr->next=employees;
			employees=Eptr;
			Eptr=NULL;
		}
		else
		{
			Eptr->next=employees;
			employees=Eptr;
			Eptr=NULL;
		}
	}

	ifile.close();


operator overloads
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
ifstream& operator >> (ifstream& ifile, Employee*& ptr)
{
	ifile >> ptr->idNum;
	ifile.ignore(1000, '\n');
	getline(ifile, ptr->name);
	getline(ifile, ptr->phoneNum);
	ifile >> ptr->age;
	ifile.ignore(1000, '\n');
	ifile.get(ptr->gender);
	ifile >> ptr->salary;
	ifile.ignore(1000, '\n');
	getline(ifile, ptr->jobTitle);

	return ifile;
}
ifstream& operator >> (ifstream& ifile, Programmer*& ptr)
{
	ifile >> ptr->depNum;
	ifile.ignore(1000, '\n');
	getline(ifile, ptr->supervisor);
	ifile >> ptr->raisePer;
	ifile.ignore(1000, '\n');
	// Assuming the file reads 0/1 for bool variables
	ifile >> ptr->CPP;
	ifile.ignore(1000, '\n');
	ifile >> ptr->java;
	ifile.ignore(1000, '\n');

	return ifile;
}

ifstream& operator >> (ifstream& ifile, SoftwareArch*& ptr)
{
	ifile >> ptr->depNum;
	ifile.ignore(1000, '\n');
	ifile >> ptr->raisePer;
	ifile.ignore(1000, '\n');
	ifile >> ptr->yearsXP;
	ifile.ignore(1000, '\n');

	return ifile;
}


inputFile.txt

928373
Cody Thompson
9497355887
23
M
100000
Programmer
5254
John Kath
3
1
1
937447
Sean Mark
9496384775
23
M
37
Employee
837556
Cory Tracy
9498981023
24
M
19000
Software Architect
9239
1
5



What am i doing wrong?
Last edited on
A dynamic_cast<T*>( foo ) returns either

* a valid T* pointer only IF foo IS-A T
or
* nullptr

Your Eptr points to a genuine Employee object.
An Employee is not a Programmer.

1
2
3
4
5
Employee * Eptr = new Programmer;
auto Pptr = dynamic_cast<Programmer*>(Eptr);
if ( Pptr ) {
  // can use Pptr
}
Topic archived. No new replies allowed.