Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
New Contributor
Posts: 5
Registered: ‎03-16-2013
My Device: BB 10
My Carrier: RIM

How to store multi class reference in one variable like id<> in objective c

Is there any functionality to store multi class reference in one variable like id<> in objective c....

plz guide.

Developer
Posts: 293
Registered: ‎10-15-2012
My Device: bb10 developer
My Carrier: Orange

Re: How to store multi class reference in one variable like id<> in objective c

Use a QObject* if all your objects derive from QObject
or QVariant otherwise
New Contributor
Posts: 5
Registered: ‎03-16-2013
My Device: BB 10
My Carrier: RIM

Re: How to store multi class reference in one variable like id<> in objective c

but i want to use own classes reference in one variable.
eg.

 

class Main

{

     void myfun (id var)

     {

     }

}

 

class A{}

class B{}

 

main.myfun(A a);

main.myfun(B b);

 

what is the solution in this case plz guide..

Highlighted
Developer
Posts: 293
Registered: ‎10-15-2012
My Device: bb10 developer
My Carrier: Orange

Re: How to store multi class reference in one variable like id<> in objective c

What you are attempting makes no sense in C++

New Contributor
Posts: 5
Registered: ‎03-16-2013
My Device: BB 10
My Carrier: RIM

Re: How to store multi class reference in one variable like id<> in objective c

Yes, I know there is no sense of c++. Its a example of java.

but I want to do like this in BB cascades/c++. I want to pass object of A class as argument of function and that same function also pass object of another class B through that argument.

eg. for that we can use Object in Java and id<> in Objective c.

 

Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: How to store multi class reference in one variable like id<> in objective c

[ Edited ]

 

As @strobejb suggested, inherit both classes from a single base class. In Qt classes usually inherit from QObject.

 

class Main
{
     void myfun (QObject *var)
     {
     }
}
 
class A: public QObject{ ... }
class B: public QObject{ ... }
 
Main main;
A a;
B b; main.myfun(&a); main.myfun(&b);

But unlike Objective C the classes aren't fully dynamic so it's not possible to call child class methods via pointer to the base class if they aren't declared in the base class. Well, strictly speaking, this is possible using Qt's Q_INVOKABLE functions, or by casting the parent back to child, but it's not the best design decision.

 

A possible solution is declaring the common interface of these two classes in a base class then inheriting from this class:

 

class Base: public QObject
{
Q_OBJECT public: virtual void f(); } class A: public Base { public: virtual void f(); } class B: public Base { public: virtual void f(); } void f(Base *p) { p->f(); } A a; B b; f(&a); f(&b);

 

I strongly recommend reading a book on C++. There are too many nuances in class inheritance topic to fully cover here. It's very different compared to Objective C & Java.

 


Andrey Fidrya, @zmeyc on twitter
Developer
Posts: 293
Registered: ‎10-15-2012
My Device: bb10 developer
My Carrier: Orange

Re: How to store multi class reference in one variable like id<> in objective c

C++ templates might be the closest you are going to get to this. Read up on 'function templates' and see if it does what you want