Can someone please help me out with this question? I am unable to understand how to perform type cast overload here since typcast is not supposed to receive any arguments so naturally any attempts at giving it one fail too.
Fill in the blank at LINE-3 to complete integer cast operator overload function signature.
#include<iostream>
using namespace std;
class intString{
int *arr;
int n;
public:
intString(int k) : n(k){} //LINE-1
int operator = (int n){ //LINE-2
return arr[--n];
}
__________________(int &k){ //LINE-3
int t;
for(int j = 0; j < k; j++){
cin >> t;
this->arr[j] = t;
}
return *this;
}
};
int main(){
int k;
cin >> k;
intString s(k);
s = k;
for(int i = 0; i < k; i++)
cout << static_cast<int>(s) << " ";
return 0;
}
I have tried
operator int()(int&k){
.
.
.
}
and
operator int(int&k){
.
.
.
}
However, that obviously didn't work. I am really at a loss at what to do here.
Edit: Here is the question: Consider the following program with the following instructions.
- Fill in the blank at LINE-1 to complete constructor definition.
- Fill in the blank at LINE-2 to complete assignment operator overload function signature.
- Fill in the blank at LINE-3 to complete integer cast operator overload function signature.
Here is the code block unedited:
#include<iostream>
using namespace std;
class intString{
int *arr;
int n;
public:
intString(int k) : ______________________{} //LINE-1
_______________{ //LINE-2
return arr[--n];
}
__________________(int &k){ //LINE-3
int t;
for(int j = 0; j < k; j++){
cin >> t;
this->arr[j] = t;
}
return *this;
}
};
int main(){
int k;
cin >> k;
intString s(k);
s = k;
for(int i = 0; i < k; i++)
cout << static_cast<int>(s) << " ";
return 0;
}
return *this;
makes no sense inoperator int
either.operator int
returns anint
andreturn *this
would require a conversion toint
but thats whatoperator int
should do. You must have misunderstood somethingoperator=
looks rather messed up too. Where did you get this code from?operator=
I've seen in the thirty or so years I've been C++-ing. But none of this makes any sense.