tags:

views:

41

answers:

2

I'm familiar with the way Qt uses D-pointers for managing data. How do I do this in my code?

I tried this method:

1) move all data into a struct 2) add a QAtomicInt to the struct 3) implement a = operator and change my constructor/deconstructor to check-up on the reference count.

The issue is, when I go to do a shallow copy of the object, I get an error about QObject declaring = as private. How then do I accomplish this?

Here's an example of my copy operator:

HttpRequest & HttpRequest::operator=(const HttpRequest &other)
{
    other.d->ref.ref();
    if (!d->ref.deref())
        delete d;
    d = other.d;
    return *this;
}

Am I going about this the wrong way?

+3  A: 

Read detailed description of QSharedDataPointer class. It should clear up a bit how you're supposed to create classes with implicit data sharing.

chalup
Gets the check mark for understanding what I'm trying to do. I don't really want to copy a QObject child I want to implement my objects the same way Qt does. But they hide a bunch of macros inside their libraries. The above link provides the info needed.
Timothy Baldridge
A: 

AFAIK QObjects are not meant to be copied around. That's why QObject has a private operator= so the question is, why do you want to declare one and if you do, does your object indeeds needs to be a QObject?

shoosh