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:
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"
usingnamespace 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;
}
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
operatorint() {} // converts Employee to int
}
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).