-1

So I am following a specific set of instructions to create a catalog-like program with a menu interface. I have a class called Dealer and I am requested to create the class object inside of main() and, in order to use the member functions inside that class, I am requested to call them via a non-member function. Here's what I'm asked to do:

int main() {
    Dealer obj1("Dealer Name");
    while(execute(menu()));
    system("pause");
    return 0;
}

where menu() is a non-member function that simply returns an int value depending on the option the user selects and execute() takes in that int and it must call upon the member functions inside Dealer class depending on what the user asks for. I understand that it is more practical to have execute() take in the object as a parameter and call its methods inside, but the instructions were very clear that the main() shown above is to be untouched.

Here are some additional instructions about menu and execute: The menu method returns an integer value which should be used by the execute method to implement the selected option. The execute method receives an integer as parameter and performs the requested action by calling upon methods of the Dealer object. If the value of the parameter is incorrect the execute method prints an error message.

The question is how can I call the Dealer member functions inside execute() once the object is created outside that function and without using the object as a parameter for the non-member function?

12
  • 2
    So at least execute() needs to receive the Dealer object as reference parameter, or you need to make it a global variable instead. Jan 29, 2022 at 14:36
  • The 'main()' function you see above is what is specifically being asked for, I can't change that.
    – kirito23
    Jan 29, 2022 at 14:43
  • Why you cannot change main? that's nonsensical requirement. What's the point of obj1 being in main if it's not used there?
    – Quimby
    Jan 29, 2022 at 14:45
  • I agree, but that was the only specific requirement of the whole task
    – kirito23
    Jan 29, 2022 at 14:46
  • There's no way to make these functions aware about your Dealer object instance in the main() functions scope, unless doing dirty tricks using static functions defined by the Dealer class. Jan 29, 2022 at 14:46

1 Answer 1

1

With the outlandish requirements you specify you can accomplish it like this:

Dealer* lastDealer;

class Dealer {
    Dealer(const char* name) {
        lastDealer = this;
        .....
    }
    bool execute(int option) {
        .....
    }
}

bool execute(int option) {
    return lastDealer->execute(option);
}

I must stress, that silently setting up some global variables inside constructors or other member functions is an antipattern. I would never ask someone to do something like this, even if teaching C++. It teaches what shouldn't be done in C++, even if possible.

4
  • As I mentioned dirty tricks with global variables. Could at least be nicely hidden into a private class static variable, or Singleton. Jan 29, 2022 at 15:09
  • @CygnusX1 Thanks for your input, I'll take it into account if I have nothing left
    – kirito23
    Jan 29, 2022 at 15:21
  • 1
    @πάνταῥεῖ No matter if global, hidden static, or something else. It's the behavior that is dangerous and should not be taught: a member function changing the state of something else but the object it works on (or any of its explicit arguments if really needed)
    – CygnusX1
    Jan 29, 2022 at 15:24
  • @CygnusX1 totally d'accord 👍 Jan 29, 2022 at 15:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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