-1

I want to create a function in C++ to get a pointer to a class with some conditions. I have multiples instances for a class, identified by a number. Depending on number, I want to get the corresponding instance.

In c# it would be something like :

class main
{
    private Example item1;
    private Example item2;
    private Example item3;
    private Example item4;

    public bool InitializeItem(int itemID)
    {
        bool isInitialized = false;
        Example item;
        if (tryGetItem(itemID, out item))
        {
            item = new Example(itemID);
            isInitialized = true;
        }
        return isInitialized;
    }

    private bool tryGetItem(int itemID, out Example item)
    {
        bool canGet = false;
        item = null;
        switch (itemID)
        {
            case 1:
                item = item1;
                canGet = true;
                break;
            case 2:
                item = item2;
                canGet = true;
                break;
            case 3:
                item = item3;
                canGet = true;
                break;
            case 4:
                item = item4;
                canGet = true;
                break;
        }
        return canGet;
    }
}

class Example
{
    int number { get; set; }
    public Example(int i)
    {
        number = i;
    }
}

But in C++ I'm a little bit confuse with referencing and pointers. I read some tutorials like this one(in french). I understood basic pointers but with classes and function i'm lost.

With first answer, I changed my code to :

Example item1;
Example item2;
Example item3;
Example item4;

bool tryGetItem(int b, Example &ptr)
{
    bool canGet = false;
    ptr = NULL;
    switch (b)
    {
    case 1:
        ptr = item1;
        canGet = true;
        break;
        /* etc */
    }
    return canGet;
}

bool InitializeItem(int id)
{
    bool isInit = false;
    Example ptr = NULL;
    if (getParam(id, ptr))
    {
        ptr = Example(id);
        isInit = true;
    }
    return isInit;
}

But it doesn't work. I've try to debug, getParam(1, ptr) is true, in the {..} the variable ptr is correctly set to 1 but item1 doesn't change.

Edit: I don't think it's the same problem that possible duplicate post. I don't want to modify ptr in tryGetItem, I want to use tryGetItem to make ptr pointing on one of my itemX. After using tryGetItem with value 1, modifying ptr must modify item1 too.

A.Pissicat
  • 3,023
  • 4
  • 38
  • 93
  • 5
    Search for "pass by reference" `&` and read a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – UnholySheep Oct 12 '17 at 12:49
  • C++ works quit differently from C#. objects are usually handled like you would handle and `int` for example in C#. object in C++ is usually not "reference to some object on the heap", but more as "objects are simply objects, as ints are simply ints" – David Haim Oct 12 '17 at 12:51
  • Generally, returning the output is preferred over an output parameter. – Passer By Oct 12 '17 at 12:52
  • by the way the C++ code you provided doesn't even compile – UnholySheep Oct 12 '17 at 12:54
  • @UnholySheep since this is C# code which he want to know how to translate to C++. – Marek R Oct 12 '17 at 13:32
  • I modified my code depending on first answer. – A.Pissicat Oct 12 '17 at 13:42
  • Possible duplicate of [When I change a parameter inside a function, does it change for the caller, too?](https://stackoverflow.com/questions/1698660/when-i-change-a-parameter-inside-a-function-does-it-change-for-the-caller-too) – dandan78 Oct 12 '17 at 13:47
  • @dandan78 I don't think it's the same problem (I edited my question). – A.Pissicat Oct 12 '17 at 14:12

1 Answers1

2

Use a reference :

bool tryGetItem(int b, Example & var)
{
}

You can use it very simple :

int main()
{
    Example var;
    tryGetItem(42, var);
}

Unless really necessary because no other solution is available, avoir using naked pointers in C++, prefer references or smart pointers. In your case, a reference is the best choice and will perfectly emulate out for you.

informaticienzero
  • 1,796
  • 1
  • 12
  • 22
  • Thanks for answer. I've try your solution but when I do `tryGetItem(2, var);` (to point to `item2`) and `var = Example(2)` the variable `var` is correctly set to 2 but `item2` doesn't not change. – A.Pissicat Oct 12 '17 at 13:36