01110100
01110100

Reputation: 833

How do you return a pointer to an object in order to use in functions that access the objects methods?

I've looked everywhere, and I cannot find a clear answer for my exact question here. Or perhaps I have found the answer, but I don't understand it completely.

I have 3 objects:

Foo Object1;
Foo Object2;
Foo Object3;

I have a function where a user chooses from a list of strings that correlate to objects. When the user inputs, say "B", I want to now perform operations on Object2.

Here is essentially what I have.

string getOption()
{
    string option;

    cout << "Option Menu" << endl;
    cout << "A - Option 1" << endl;    // Object1
    cout << "B - Option 2" << Lendl;   // Object2
    cout << "C - Option 3" << endl;    // Object3

    cin >> option;

    return option;

Now that I have the input, I want to perform numerous options on the object. Something like:

void modifyFooString(foo userSelectedObject)
{
    // Local variables
    string userInput;


    userInput = readInput(); // Function that reads valid input

    userSelectedObject.setMethod(userInput);
}

But with numerous functions. It's in a while loop, so I need to modify the object and then re-prompt the user if they want to make changes to a different object.

When I look at this problem, I think a pointer can be used. For example:

void getOption(Foo* &choice)
{
    string option;

    cout << "Option Menu" << endl;
    cout << "A - Option 1" << endl;    // Object1
    cout << "B - Option 2" << Lendl;   // Object2
    cout << "C - Option 3" << endl;    // Object3

    cin >> option;

    if (option == "A")
        choice = &Object1;

    else if (option == "A")
        choice = &Object2;

    if (option == "A")
        choice = &Object3;
}

So ideally, I can use the pointer now in the functions proceeding the getOption() function to modify the selected object. However, when I try to implement it like this, it's just not working.

How do I return a pointer that can be used to modify a selected object?

Thanks

Upvotes: 0

Views: 86

Answers (3)

Akhil V Suku
Akhil V Suku

Reputation: 912

A minimal example for your problem,

#include "stdafx.h"
#include <iostream>
#include <conio.h>

using namespace std;
class Foo
{
public:
    int id;
    int data;
    Foo(int tid,int tdata)
    {
        id = tid;
        data = tdata;
    }
};

Foo *Object1 = new Foo(1,0);
Foo *Object2 = new Foo(2,0);
Foo *Object3 = new Foo(3,0);

Foo* getOption()
{
    char option;

    cout << "Option Menu" << endl;
    cout << "A - Option 1" << endl;    // Object1
    cout << "B - Option 2" << endl;   // Object2
    cout << "C - Option 3" << endl;    // Object3

    cin >> option;

    switch (option)
    {
    case 'A':
        return Object1;
        break;
    case 'B':
        return Object2;
        break;
    case 'C':
        return Object3;
        break;
    default:
        cout << option ;
        break;
    }
        return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
    cout << (getOption())->id;
    _getch();
    return 0;
}

In this example you will answer

1 for option == A

2 for option == B

3 for option == C

The main changes I have done are,

1. changed

    if (option == "A") // comparing A
        choice = &Object1;

    else if (option == "A") // Comparing B
        choice = &Object2;

    if (option == "A") //comparing C
        choice = &Object3;

to 

  switch (option)
    {
    case 'A':
        return Object1;
        break;
    case 'B':
        return Object2;
        break;
    case 'C':
        return Object3;
        break;
    default:
        cout << option ;
        break;
    }

2. changed
 option == "A" to  option == 'A' format // when I have used if instead of switch

try this and let me know if it is helpful for you

Upvotes: 0

Lingxi
Lingxi

Reputation: 14967

Same as john's answer, but with the if-else-ladder being replaced by something I think clearer :)

Foo* getOption() {
   ...
   return option == "A" ? &Object1 :
          option == "B" ? &Object2 :
          option == "C" ? &Object3 :
                          nullptr  ;
}

Upvotes: 0

john
john

Reputation: 87959

You might have more success with

if (option == "A")
    choice = &Object1;
else if (option == "B")
    choice = &Object2;
else if (option == "C")
    choice = &Object3;

You should also clarify what 'it's just not working' actually means.

Since you want to return something, it more natural to use a C++ return statement.

Foo* getOption()
{
    ...
    if (option == "A")
        return &Object1;
    else if (option == "B")
        return &Object2;
    else if (option == "C")
        return &Object3;

Upvotes: 1

Related Questions