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
Regular Contributor
Posts: 70
Registered: ‎09-06-2012
My Device: Developer
My Carrier: Bell Mobility

C++ Reference Operator & usage clarification

I'm confused by some syntax in some examples using the reference operator. When I try to use code an integrate into my code I get segment faults.

 

Here is call to constructor

   AnObjectConstructor(AnotherObject->getObject(), this);  

        // returns reference to an member (I guess), I don't know what member is - no documentation

        // I wondering if it itself is a pointer

 

Constructor is defined as

 

AnObjectConstructor ( ObjectA   &objectA, QObject *parent)  

           // objectA is reference to an ObjectA, that is an address i.e. not  the object member passed by value

      : QObject(parent) 

     , m_objectA(&objectA)  

               // what does & mean here does m_objectA get assigned the address passed or the address at the address

passed. Is this a reference operator or an address of operator.

{

}

where m_objectA is defined as   ObjectA *m_objectA;

 

Why is m_objectA defined as a pointer and not simply a reference?

 

Why not pass a pointer to a pointer or a reference to a reference, why a reference to a pointer. 

 

Hope  someone can explain this syntax.

 

 

Developer
Posts: 124
Registered: ‎01-29-2011
My Device: Z10, PlayBook
My Carrier: Koodo

Re: C++ Reference Operator & usage clarification


pskarton wrote:

...

AnObjectConstructor ( ObjectA   &objectA, QObject *parent)  

           // objectA is reference to an ObjectA, that is an address i.e. not  the object member passed by value

      : QObject(parent) 

     , m_objectA(&objectA)  

               // what does & mean here does m_objectA get assigned the address passed or the address at the address

passed. Is this a reference operator or an address of operator.

{

}

where m_objectA is defined as   ObjectA *m_objectA;

 

Why is m_objectA defined as a pointer and not simply a reference? 


That seems strange to me. Is there another constructor that does not accept objectA? AnObject should be able to have "ObjectA &m_objectA" as a member and then use accept it with "AnObject::AnObject(ObjectA &objectA) : m_objectA(objectA)". At least, that's what I did when I had something similar. Otherwise, as you ask, why not just accept a pointer to obejctA?

 

What's the definition of AnotherObject->getObject()? Is it "ObjectA &getObject()"? Otherwise, it seems you're doing something to pass a pointer to something in as a reference. If so, you should do a NULL check first.