Type casting reference to object to derived class?

Back to topics list To post a new topic, please log in or register
avatar
31
wqking 2015.10.03 10:07
 
class Base
{
};

class Derived : public Base
{
public:
    void MyFunc(const Base & obj)
    {
        // how to type cast obj to Derived?
    }
};


As the code above, the obj passed to MyFunc is indeed Derived. So how can I type cast it to Derived?

Also, seems reference '&' can only be used as function parameter? It can't be used to declare local variable?


Thanks


avatar
13009
WHRoeder 2015.10.03 15:18 #
 
  1. Reference is how something is passed to a function. You can't used it to create a pointer like C/C++.
  2. If your function takes a Base, why would you want to cast it? If you function needs a Derived, that is what the parameter should be.
  3. Also if you pass something other than a Derived, and you try to cast it, the code dies (bad cast) just like a divide by zero.
  4. See dynamic_cast - MQL4 forum where I pass a pointer by reference, cast a Base to Derived and can avoid the bad cast.
  5.  void MyFunc(const Base & obj)
    While const is how you should pass them, you can't cast const pointers
    derive* d = (const derive*)b;
    'const' - unexpected token      testscr.mq4     67      16
    
avatar
31
wqking 2015.10.03 16:03 #
 
WHRoeder:
  1. Reference is how something is passed to a function. You can't used it to create a pointer like C/C++.
  2. If your function takes a Base, why would you want to cast it? If you function needs a Derived, that is what the parameter should be.
  3. Also if you pass something other than a Derived, and you try to cast it, the code dies (bad cast) just like a divide by zero.
  4. See dynamic_cast - MQL4 forum where I pass a pointer by reference, cast a Base to Derived and can avoid the bad cast.
  5. While const is how you should pass them, you can't cast const pointers

2, I want to pass Base for polymorphic. Indeed MyFunc is a virtual function defined in the Base class and overrided by Derived. In my example, MyFunc accepts a Derived object indeed, that's why I want to cast it.

4, The example is about pointer. My more questions: is pointer only used for dynamically allocated objects (new Derived) etc? I put Derived object in an array, not allocating it on heap.

Any detailed explanation or documentation on reference/pointer in MQL, or compare to C++ (I have deep understanding in C++)?

Thanks for your reply.


avatar
13009
WHRoeder 2015.10.03 16:21 #
 
  1. If the function is defined in the base then call it, no cast is required; that is the purpose of virtual. As a matter of good programming, all virtual functions should be private.
  2. Your function does not accept a derive object it accepts a base.
     void MyFunc(const Base & obj)
  3. A pointer can be used for either, but you must assure that the stack object outlives the pointer.
  4. In C++ the ampersand is both by reference in function calls, and to create pointer. In MQ4 to create a pointer you use GetPointer - MQL4 Documentation



avatar
31
wqking 2015.10.04 02:33 #
 

GetPointer solved my problem.

Thank you very much.

avatar
62
nikolaygmt 2016.02.24 00:45 #
 
Hi wqking, could you provide source code of your solution? Thank you very much!
avatar
62
nikolaygmt 2016.02.24 03:05 #
 

I believe I found it out:

Derived *derivedObj = (Derived*) GetPointer(baseObj);

Back to topics list  

To add comments, please log in or register