0

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;
}
20
  • remove the parameter? Who gave you the task and what is the exact wording? Mar 16, 2023 at 16:19
  • 1
    return *this; makes no sense in operator int either. operator int returns an int and return *this would require a conversion to int but thats what operator int should do. You must have misunderstood something Mar 16, 2023 at 16:21
  • operator= looks rather messed up too. Where did you get this code from? Mar 16, 2023 at 16:22
  • 1
    That's by far the worst abuse of operator= I've seen in the thirty or so years I've been C++-ing. But none of this makes any sense.
    – molbdnilo
    Mar 16, 2023 at 16:54
  • 3
    Right, so they mixed up the hints for 2&3. LINE-2 is supposed to be cast-to-int and LINE-3 is assign-from-int. But the whole thing is really cursed and bad.
    – teapot418
    Mar 16, 2023 at 17:10

2 Answers 2

2

After reading the code below // LINE 3, it seems like the question mixed up what line LINE 2 and LINE 3 are supposed to do. The block returns *this, which is an object of type intString, not an integer. Furthermore, user-defined type casts or type overloads like these don't allow a param to be passed to them, so k won't be defined in the scope of that method.

Moreover, a look at the driver code suggests that the assignment s = k; should take some user input that sets the values in arr, which can then be fetched by the loop below. At LINE 2, n shouldn't be passed as it's a data member of the class...

I think the question got flipped for LINE 2 and LINE 3.

So, to sum up LINE 1, LINE 2 and LINE 3 should probably be -

LINE-1:

intString(int k) : n(k), arr(new int[k]) {

LINE-2:

operator int() {

LINE-3:

intString operator= (int &k) {

I've purposefully left the above spoiler tagged, in case you want to solve this yourself.

2
  • 1
    & is missing in one case!
    – Marek R
    Mar 16, 2023 at 17:22
  • Oh thanks, @MarekR. I didn't realise that initially. LINE-3 should return a reference to intString instead of returning by value to conserve resources
    – Vyvy-vi
    Mar 16, 2023 at 17:44
1

It seems you mean the conversion operator.

For example it can look the following way

operator const int & () const 
{
    return n;
}

or just

operator int () const
{
    return n;
}

These operators may be defined with specifier explicit

explicit operator const int & () const 
{
    return n;
}

or

explicit operator int () const
{
    return n;
}

Pay attention to that the data member arr

class intString{
    int *arr;
    //...

is not initialized. So your code can invoke undefined behavior.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.