Search code examples
c++qtblackberry-10blackberry-cascades

Passing a const reference of a NavigationPane results in error


Using BlackBerry 10 Cascades, I create a class:

MyClass : public QObject
{
 QObject
  public:
    MyClass(const bb::cascades::NavigationPane & navigationPane);
    //...
  protected:
    bb::cascades::NavigationPane m_navigationPane;

In the ccp file I have this in the constructor:

MyClass::MyClass(const bb::cascades::NavigationPane & navigationPane)
{
    m_navigationPane = navigationPane;

When I build, it gives me the error:

'bb::cascades::NavigationPane& bb::cascades::NavigationPane::operator=(const bb::cascades::NavigationPane&)' is private


Solution

  • Without knowing that class, it is clear that it can't be copied. The error message is quite clear: its copy assignment operator is private. This is usually done on purpose to avoid copying.

    From its name I guess that it represents a real resource; it makes no sense copying the object, because the resource is unique.

    I understand that what you need is really the object you are getting in the constructor. Assuming that it will last as long as the MyClass instance you are creating with it, you can use a reference member in your class. This can only be initialised in the initialisation list of the constructor, this way:

    MyClass : public QObject
    {
     QObject
      public:
        MyClass(const bb::cascades::NavigationPane & navigationPane);
        //...
      protected:
        const bb::cascades::NavigationPane& m_navigationPane;
    

    And then the constructor will be:

    MyClass::MyClass(const bb::cascades::NavigationPane & navigationPane)
    : m_navigationPane(navigationPane)
    {
    //    m_navigationPane = navigationPane;
    

    Of course, if you need to actually modify the NavigationPane instance from inside your MyClass object, you just have to use non-const references and everything will work fine.