Swapping pointers

Could anyone teach me how to swap 2 pointers/ correct my mistakes

Employee *staff[20]
1
2
3
4
5
6
7
8
9
10
11
for(int i = 0 ; i < count ; i++)
	{
		Employee *temp ;
		for(int j = 0 ; j < count-1-i ; j++)
		{
			if(strcmp(staff[i]->getname(),staff[i+1]->getname())>0)
			*temp = staff[i+1] ;
			staff[i+1] = staff[i] ;
			staff[i] =  *temp ;
		}
	}
Well, the question is: Do you want to swap the content where the pointer are pointing to or the pointer itself and leave the content at its current position? I assume the second.

1
2
3
4
5
6
7
8
9
// swap the content. Probably not what you meant?
*temp = *staff[i+1] ;
*staff[i+1] = *staff[i] ;
*staff[i] =  *temp ;

// swap only the pointer and leave the Employee where it is
temp = staff[i+1] ;
staff[i+1] = staff[i] ;
staff[i] =  temp ;



By the way, you can use std::swap, which is a bit easier to understand what you mean ;)

1
2
std::swap( *staff[i], *staff[i+1] ); // swap Employees and leave pointer where they are.
std::swap( staff[i], staff[i+1] ); // swap pointer 


Ciao, Imi.
Hi, Imi thanks for your help. It is not the latter that I want, but thanks for giving both. I wish i could use the std::swap, however i am required to know both of them as it is coming out for my test.

With Regards
DoomCarnage
Last edited on
*Update*

I more question:

1
2
3
4
5
6
7
8
Employee *temp ;
		for(int j = 0 ; j < count-1-i ; j++)
		{
			if(strcmp(staff[i]->getname(),staff[i+1]->getname())>0)
			*temp = *staff[i+1] ;
			*staff[i+1] = *staff[i] ;
			*staff[i] =  *temp ;
		}


What should i let my *temp pointing to ?
Is it Employee *temp = new Employee? (its obviously wrong, laughs)
I post my whole programme up in case if you need any reference

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
int main ( ) 
{ 
    Direct Dl ("TOUCHAPHOL ",25,1234,1000,20) , D2 ; 
    Indirect ID1 ( " WINSTON SEETOH " , 21,2345, 10,56) , ID2 ; 
	Employee *staff[SIZE] ; 

	cin >> D2;    // read data from keyboard into a Direct object
    cin >> ID2;   // read data from keyboard into a InDirect object 
	staff[0] = &Dl;     
	staff[1] = &D2; 
	staff[2] = &ID1;    
	staff[3] = &ID2; 
    int count = 2; 

	ifstream in ;
	in.open("direct.dat",ios::in)  ;
	if( in.fail())
	{
		cerr<<"the file does not exist" ;
		system("pause") ;
		exit(1) ;
	}

	while(!in.eof())
	{
		Direct *Directp = new Direct  ;
		in >> *Directp ;
		staff[count] = Directp ;
		count++ ;
	} 

	in.close() ;
	
	in.open("indirect.dat",ios::in)  ;
	if( in.fail())
	{
		cerr<<"the file does not exist" ;
		exit(1) ;
	}

	while(!in.eof())
	{
		Indirect *indirectp = new Indirect  ;
		in >> *indirectp ;
		staff[count] = indirectp ;
		count++ ;
	} 

	in.close() ;
	
	//Unsorted Data
	cout << setiosflags(ios::left|ios::fixed) 
		 << "**Unsorted Data** ";
	cout <<"\nEmployee Particulars" ;
	cout << setw(10) << "\nType"  << setw(20) << "Name" << setw(5)  << "Age"<< setw(6) << "ID" 
		 << setw(12)  << "Salary/Hour"	<<setw(9) << "CPF/Rate" << setw(3) << "Pay" 
		 <<"\n--------------------------------------------------------------------------------" ; 
	for (int i = 0 ; i < count ; i++)
	{
		if(staff[i] ->whoami() == DIRECT) 
		{
			static_cast<Direct*>(staff[i]) ->cal_pay() ;
			staff[i] ->display() ;
		}
		else
		{
			static_cast<Indirect*>(staff[i]) ->cal_pay() ;
			staff[i] ->display() ;
		}
	}
	cout <<"\n--------------------------------------------------------------------------------" ;
	cout <<endl ;
	system("pause") ;
	system("cls") ;

	//Sorting Data
	for(int i = 0 ; i < count ; i++)
	{
		Employee *temp ;
		for(int j = 0 ; j < count-1-i ; j++)
		{
			if(strcmp(staff[i]->getname(),staff[i+1]->getname())>0)
			*temp = *staff[i+1] ;
			*staff[i+1] = *staff[i] ;
			*staff[i] =  *temp ;
		}
	}

	//After sorted
	cout << setiosflags(ios::left|ios::fixed) 
		 << "**Unsorted Data** ";
	cout <<"\nEmployee Particulars" ;
	cout << setw(10) << "\nType"  << setw(20) << "Name" << setw(5)  << "Age"<< setw(6) << "ID" 
		 << setw(12)  << "Salary/Hour"	<<setw(9) << "CPF/Rate" << setw(3) << "Pay" 
		 <<"\n--------------------------------------------------------------------------------" ; 
	for (int i = 0 ; i < count ; i++)
	{
		if(staff[i] ->whoami() == DIRECT) 
		{
			static_cast<Direct*>(staff[i]) ->cal_pay() ;
			staff[i] ->display() ;
		}
		else
		{
			static_cast<Indirect*>(staff[i]) ->cal_pay() ;
			staff[i] ->display() ;
		}
	}
	cout <<"\n--------------------------------------------------------------------------------" ;
	cout <<endl ;
	system("pause") ;


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
43
44
45
46
class Employee
{
protected:
	char name[20];
	float age;
	int Eid;
	float pay;
public:
	Employee(void); // default constructor function
	Employee(char *nam, float ag, int id, float p); // constructor fn
	char *getname (void) {return name ;}; // return name
	virtual int whoami() = 0;
	virtual void cal_pay() = 0; // calculate pay; pure function
	virtual void display() = 0; // pure virtual function
};

class Direct : public Employee
{
	float basic; // basic salary
	float cpf_rate ; // cpf contribuation rate
public:
	Direct() ; // constructor
	Direct( char * nam, float ag, int id, float bas, float r);
	int whoami(); // to determine whether the employee is Direct/Indirect
	void cal_pay() ;// calculate pay
	void display(); // display a Direct object
	friend istream& operator >> (istream& op, Direct& someDirect);
	friend ifstream& operator >> (ifstream& , Direct& someDirect);
	friend ofstream& operator << (ofstream& , Direct& someDirect);
};


class Indirect : public Employee
{
	float hours;
	float rate;
public:
	Indirect();			//----constructor function
	Indirect(char *nam, float ag, int id, float rat, float hr);//constructor function
	int whoami();
	void cal_pay();		//---- Calculate the pay of the Indirect
	void display(void);	//---- displays the Indirect object
	friend istream& operator>>(istream& op, Indirect& someIndirect);
	friend ifstream& operator>>(ifstream&, Indirect& someIndirect);
	friend ofstream& operator<<(ofstream& op, Indirect& someIndirect);
}; 


Once again, thanks.
With Regards
DoomCarnage
You could not use a pointer to a temporary Employee at all. Just declare one directly (on the stack):

1
2
3
4
5
Employee temp;
...
temp = *staff[i+1] ;
*staff[i+1] = *staff[i] ;
*staff[i] =  temp ;


Ciao, Imi.
Topic archived. No new replies allowed.