-2

I wrote some code with some function that get pointer and some const and need to return the element that its point on =

I can't find the reason that my pointer is null on return from the function - any help please

    class MyClass 
    {
        // Some Code    
    }

    void FindCurrectMyClass(const int val, MyClass* myClassPtr)
    {
        switch(val)
        {
            case(0):
            {
                myClassPtr = &myClass0;
            }
            break;

            case(1):
            {
                myClassPtr = &myClass1;     // get here and myClassPtr is not null
            }
            break;

            case(2):
            {
                myClassPtr = &myClass2;
            }
            break;
        }
    }

    void main()
    {
        MyClass myClass0;
        MyClass myClass1;
        MyClass myClass2;   

        MyClass* myClassPtr = nullPtr;

        FindCurrectMyClass(1, myClassPtr );

        myClassPtr->Func();     // myClassPtr is null 
    }
Yanshof
  • 9,659
  • 21
  • 95
  • 195
  • 2
    Your code doesn't compile, try to put together a working example. Besides, you are passing the pointer by value, and the assignments `myClassPtr = &myClass` mutate the copy in the function scope, not the one in `main`. – lubgr Jan 21 '19 at 12:14
  • Your code doesn't compile because there is no declaration for `myClass0` visible to `FindCurrectMyClass`. My guess would be that you've defined your variables twice, once globally and once inside `main`. That would explain why the pointers aren't what you expect them to be. – john Jan 21 '19 at 12:15
  • the code compile - and the value is on .. 3 instance of myClass .. no – Yanshof Jan 21 '19 at 12:20
  • @Yanshof I'm sure **your** code compiles but the code you have posted does not. We often have this problem, people post part of their code and miss out vital pieces. – john Jan 21 '19 at 12:25

1 Answers1

1

edit, I imagined we are in C, but we are in C++

replace

void FindCurrectMyClass(const int val, MyClass* myClassPtr)

by

void FindCurrectMyClass(const int val, MyClass*& myClassPtr)

else you only modify the local parameter so when you go back nothing is done and myClassPtr is still null


Of course you have to put

MyClass myClass0;
MyClass myClass1;
MyClass myClass2;   

for instance at global scope before FindCurrectMyClass else that one cannot access them

bruno
  • 32,421
  • 7
  • 25
  • 37
  • Surely better to return the pointer rather than set via a parameter. – john Jan 21 '19 at 12:16
  • @john sure; but I just fixe the problem without doing more change in the philosophy, to indicate to the O.P. how to use an output parameter – bruno Jan 21 '19 at 12:17
  • this not solve me anything because i don't want to use pointer to pointer - and one pointer is need to be ok – Yanshof Jan 21 '19 at 12:21
  • @Yanshof you are copying in the pointer, you can't change the outside pointer if you don't pass it as either `MyClass*&` or `MyClass**` – PeterT Jan 21 '19 at 12:23
  • @Yanshof that solve your problem. I think you just you do not understand the difference between a pointer and an object. If you want something more simple as john says do not use an output parameter but return the address – bruno Jan 21 '19 at 12:23
  • 1
    @Yanshof ah yes we are in C++, I supposed we are in C, I edited my answer – bruno Jan 21 '19 at 12:25
  • Can someone explain why void FindCurrectMyClass(const int val, MyClass* myClassPtr) does not work and void FindCurrectMyClass(const int val, MyClass*& myClassPtr) is working ?? ?? – Yanshof Jan 21 '19 at 12:37
  • @Yanshof This was explained multiple times for you, in the comments. But, if that's not enough, you can read about [the difference between passing by reference vs. passing by value](https://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value). – Algirdas Preidžius Jan 21 '19 at 12:41