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
Super Contributor
Posts: 440
Registered: ‎01-13-2011
My Device: Playbook
Accepted Solution

Access a class later on?

I have my own BBM class which I create in main:

 

BbmConnection *bbmConnection = new BbmConnection(uuid, &app);
    bbmConnection->RegisterApplication();

 How can I use this class later on in my app, in another class?

 

I can't reintialise it becuase I don;t know what uuid and app are later on in the app.

 

I'm using C++

 

Thanks

 

 

--------------------------------------------------------------------------------------------------
BlackBerry Apps: Instruments | ARTPAD | Piano | Drums | Xylophone
Developer
Posts: 1,523
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: Access a class later on?

[ Edited ]

One of the solutions is to use a singleton.

 

Remove uuid & app arguments from the constructor.

 

In BbmConnection.h add:

 

static BbmConnection *sharedInstance();

 In BbmConnection.cpp:

BbmConnection *BbmConnection::sharedInstance()
{
  static BbmConnection instance;
  return &instance;
}

Add a method for intialization, for example init(). Call it once in main.cpp:

 

BbmConnection::sharedInstance()->init(uuid, &app)

 

In other classes:

 

BbmConnection::sharedInstance()->otherMethods();

 

p.s. If app is an application instance then it  can be accessed from any method by calling Application::instance().

 

 


Andrey Fidrya, @zmeyc on twitter
Super Contributor
Posts: 440
Registered: ‎01-13-2011
My Device: Playbook

Re: Access a class later on?

Thanks, do I need to still intialise the class with:

 

BbmConnection *bbmConnection = new BbmConnection();

 Or is there a better way to do it?

--------------------------------------------------------------------------------------------------
BlackBerry Apps: Instruments | ARTPAD | Piano | Drums | Xylophone
Developer
Posts: 1,523
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: Access a class later on?

[ Edited ]

No need to initialize it explicitly, it will be automatically created on first call to sharedInstance():

 

BbmConnection *BbmConnection::sharedInstance()
{
  static BbmConnection instance;
  return &instance;
}

Local static variables are created on first call to the function and aren't destroyed on exit from the function. They work like global variables, so it's safe to return a pointer to instance here.

 

The code above is roughly equivalent to:

 

BbmConnection *BbmConnection::sharedInstance()
{
  static BbmConnection *instance = new BbmConnection;
  return instance;
}

 

 

You just need to initialize class variables in init() method (this method should be non-static!)

 

Something like this (pseudocode):

 

void BbmConnection::init(sometype someparam)

{

  mSomeparam = someparam;

}

 

And call init() once before using the class in other places of code.

 

I hope my explanation wasn't too confusing. :smileyhappy:


Andrey Fidrya, @zmeyc on twitter
Super Contributor
Posts: 440
Registered: ‎01-13-2011
My Device: Playbook

Re: Access a class later on?

Perfect, thanks for the detailed explanation.

 

You have been most helpful.

--------------------------------------------------------------------------------------------------
BlackBerry Apps: Instruments | ARTPAD | Piano | Drums | Xylophone
Super Contributor
Posts: 440
Registered: ‎01-13-2011
My Device: Playbook

Re: Access a class later on?

[ Edited ]

I seem to have hit a new problem:

 

BbmConnection::BbmConnection(const QUuid &uuid, QObject *parent) : QObject(parent), m_context(uuid), m_isAllowed(false), m_progress(BbmRegistrationProgress::NotStarted), m_temporaryError(false), m_statusMessage(tr("Please wait while the application connects to BBM."))

 This was the old way I intialised the class, I cant add this to init as it's invalid code:

 

only constructors take member initializers

 

 

How can I add this to the init method?

--------------------------------------------------------------------------------------------------
BlackBerry Apps: Instruments | ARTPAD | Piano | Drums | Xylophone
Developer
Posts: 1,523
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: Access a class later on?

References can be initialized only in constructor. But it should be possible to copy the uuid:

 

Replace "const QUuid &m_context" with "QUuid m_context" in .h file.

 

void BbmConnection::init(const QUuid &uuid)
{
  m_context = uuid;
  m_isAllowed = false;
  m_progress = BbmRegistrationProgress::NotStarted;
  m_temporaryError = false;
  m_statusMessage = tr("Please wait while the application connects to BBM.");
}

 Constructor will look like this:

BbmConnection::BbmConnection(QObject *parent) : QObject(parent)
{
...
}

 


Andrey Fidrya, @zmeyc on twitter
Super Contributor
Posts: 440
Registered: ‎01-13-2011
My Device: Playbook

Re: Access a class later on?

Thanks but I get the error:

 

no match for 'operator=' in '((BbmConnection*)this)->BbmConnection::m_context = uuid'

 Context is not of type of QUuid, it's:

 

bb::platform::bbm::Context m_context;

 Any ideas of how to set it. I've tried the other ways

 

m_context(uuid);

 

no match for call to '(bb::platform::bbm::Context) (const QUuid&)'

 Any ideas?

--------------------------------------------------------------------------------------------------
BlackBerry Apps: Instruments | ARTPAD | Piano | Drums | Xylophone
Developer
Posts: 1,523
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: Access a class later on?

[ Edited ]

Change m_context to:

 

bb::platform::bbm::Context *m_context;


... in init():

m_context = new bb::platform::bbm::Context(uuid, this);


The context is now a pointer. When it was referenced like this:
m_context.something() change to: m_context->something()

if it was passed to a function:
func(m_context) change to: func(*m_context)


Andrey Fidrya, @zmeyc on twitter
Super Contributor
Posts: 440
Registered: ‎01-13-2011
My Device: Playbook

Re: Access a class later on?

If I do this, it breaks the rest of the class, the original working line is:

 

m_messageService = new bb::platform::bbm::MessageService(&m_context, this);

 I know you say this should change to:

 

m_messageService = new bb::platform::bbm::MessageService(*m_context, this);

 But this gives the compile error:

 

no matching function for call to 'bb::platform::bbm::MessageService::MessageService(bb::platform::bbm::Context&, BbmConnection* const)'

 So I change it to:

 

m_messageService = new bb::platform::bbm::MessageService(m_context, this);

 No ccompile errors but now the BBM invite to download doesnt show on screen.

--------------------------------------------------------------------------------------------------
BlackBerry Apps: Instruments | ARTPAD | Piano | Drums | Xylophone