overload typecasting operator

How to overload the typecasting operator, I wrote an overloaded version of it, but it has no effect. It turns out the program is just using the 1 - args constructor for the implicit typecast at the line:

g = static_cast <Employees> (2);


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

header.hpp:

#pragma once

#include <string>


struct Employee{

	std::string name;
	double salary;

};


// struct and classes can not have the same name 
class Employees {

public: 

	// don't forget std:: infront of string 
	std::string name_;
	float salary_;

	// 2 Ags contsturctor which needs to be implemented
	Employees(std::string namez, float salary) : name_(namez), salary_(salary) {}

	// default constructor which needs to be implemented,after 2 args constructor has been implemented 
	Employees() :name_("test"), salary_(0) {}

	// 1 args constructor used for implicit cast 
	Employees(int a) :name_(" testing "), salary_(a) {}

	Employees const   func1(Employees const &  e)
	{
		Employees d;
		d.name_ = e.name_;
		d.salary_ = e.salary_ + 1; 

		//e.salary_ = e.salary_ + 2;  //e is const , it is nonmodifieable 

		return d; 	
	};

	// type cast operator for implicit conversion
	operator Employees()
	{
		Employees e;
		e.name_ = "test";
		e.salary_ = 1;
		return e; 
	}

};



Source.cpp:

# include <iostream>
# include <string>
# include "header.hpp"

using namespace std;

int main() {

	string test = "abb";

	Employees emp( test  , 23.21);  // calls 2 args constructor 

	Employees e;  // calls default constructor , then sets the fields 
	e.name_ = test;
	e.salary_ = 23; 

	Employees d = e.func1(e); 

	cout << " salary of d is " << d.salary_ << endl;

	d.salary_ += 1; // even though d is const it is modifieable ? 

	cout << " salary of d is " << d.salary_ << endl;

	Employees g;

	// explicit cast int 2 to an ojbect of employees through the implicit cast operator and the 1 args constructor 
	g = static_cast <Employees> (2);

	cout << " name  of g is " << g.name_ << endl;
	cout << " salary of g is " << g.salary_ << endl;

	Employees f;

	int a = 3;
	// implicit cast int 2 to an ojbect of employees through the implicit cast operator and the 1 args constructor 
	f =  a;

	cout << " name  of g is " << g.name_ << endl;
	cout << " salary of g is " << g.salary_ << endl;





}
Last edited on
How to overload the typecasting operator,

There is no such thing as a typecasting operator. What you seem to be referring to is called the function call operator.

https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.2.0/com.ibm.zos.v2r2.cbclx01/cplr327.htm

There is no such thing as a typecasting operator

See user-defined conversions:
http://en.cppreference.com/w/cpp/language/cast_operator
Note that inside class Employee, a conversion operator to Employee tells the compiler how to convert objects of type Employee... to objects of type Employee.

If you want to allow implicit conversion of objects of type int to objects of type Employee, you would define a converting constructor accepting int (i.e., a one-argument non-explicit constructor for Employee accepting int).
If you want to allow implicit conversion of objects of type Employee to type int, you would define an implicit conversion operator int().

For example:
1
2
3
4
class Employee { 
  Employee(int) {} // converts int to Employee
  operator int() {} // converts Employee to int
}

Last edited on
If you want to allow implicit conversion of objects of type Employee to type int, you would define an implicit conversion operator int().


so how about if i want to convert an int to an employee is there a typecasting operator for that ?
If i want to convert an int to an employee is there a [conversion operator] for that?

No. There's a converting constructor, though.

mbozzi wrote:
If you want to allow implicit conversion of objects of type int to objects of type Employee, you would define a converting constructor accepting int (i.e., a one-argument non-explicit constructor for Employee accepting int).
Last edited on
(i.e., a one-armguent non-explicit constructor for Employee accepting int).


in terms of constructor what does the term non explicit mean ?
explicit is a language keyword which disallows implicit conversions. Examples here:
http://en.cppreference.com/w/cpp/language/explicit

Even if we don't discuss what the keyword does, "non-explicit" just means it isn't used.

For instance
1
2
struct A { explicit A(int) {} /* explicit constructor */ }; 
struct B { B(int) {} /* non-explicit constructor */ };  
Last edited on
Topic archived. No new replies allowed.