4

I've been trying to understand how to pass this as a const reference.

I have the following class:

class DBContext : public QObject

In my class MainWindow I define it as folows:

private:
    QScopedPointer<DBContext> dbContext;

How Storage class is defined:

class StorageData : public QObject
{
    Q_OBJECT

public:
    StorageData(QObject *parent = 0);
    ~StorageData();

    void SetDBContext(const QScopedPointer<DBContext> &db_context);

private:
    QScopedPointer<DBContext> dbContext;

In StorageData SetDBContext() I try to assign it with the following:

void StorageData::SetDBContext(const QScopedPointer<DBContext> &db_context)
{
    dbContext = db_context;
}

Then in the MainWindow constructor initialize it:

dbContext.reset(new DBContext(this));
StorageData storage;
storage.SetDBContext(dbContext);

Using the following in StorageData function SetDBContext():

_DBContext = db_context; 

I get an error:

'QScopedPointer>::operator =' : cannot access private member declared in class 'QScopedPointer>'

Update

Taking the tip of using QSharedPointer and QWeakPointer. Is this a valid approach of using these two pointers? Also am I using clear() correctly on both Class A & B?

class A
private:
  QWeakPointer<DBContext> dbContext;

void A::~A()
{
     dbContext.clear()
}
void A::SetDBContext(QWeakPointer<DBContext> db_context)
{
    dbContext= db_context;
}

void A::UseCode()
{
    QSharedPointer<DBContext> ptrContext= dbContext.toStrongRef();
    ..
}

class B constructor:
private:
    QSharedPointer<DBContext> dbContext; 

In the constructor:

B::B()
{
    dbContext.reset(new DBContext(this));
    QWeakPointer<DBContext> ptrDBConnect = dbContext;
    storage.SetDBContext(ptrDBConnect);
}

In the destructor:

B::~B()
{
    dbContext.clear();
}
demonplus
  • 5,613
  • 12
  • 49
  • 68
adviner
  • 3,295
  • 10
  • 35
  • 64

1 Answers1

2

From the documentation on QScopedPointer:

QScopedPointer intentionally has no copy constructor or assignment operator, such that ownership and lifetime is clearly communicated.

The purpose of QScopedPointer is to keep the lifetime of the object you are pointing to tied to the scope of the QScopedPointer. If you could assign/copy it, then it defeats the whole point.

MahlerFive
  • 5,159
  • 5
  • 30
  • 40
  • What Qt smartpointer is best using for passing to different class by reference only? I dont want the destructor called from the class its passed to. I only want it called from the class that initiated it. In this case MainWindow – adviner Nov 19 '15 at 01:38
  • 1
    One option is to use `QSharedPointer`. You can copy them, and only when all of the copies go out of scope will the object be destroyed. – MahlerFive Nov 19 '15 at 01:46
  • It sounds like just a raw pointer to me, if you don't want to worry about lifetime control at all. – ajshort Nov 19 '15 at 02:34
  • If you don’t want shared ownership but smart pointers, you could pass it as QSharedPointer but store it as QWeakPointer. Or, if you use C++11 and want to pass the ownership, use a std::unique_pointer and move semantics. – Frank Osterfeld Nov 19 '15 at 09:09
  • I've updated my code to use QSharePointers and QWeakPointers. If my approach valid? – adviner Nov 19 '15 at 13:01