This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Adding new data members to classes without d-pointer

Tags: None
(comma "," separated)
cafu
Registered Member
Posts
1
Karma
0
Hi All,

Greetings my first post, hopefully helpful for someone else besides myself.

I am facing difficulties trying to understand the wiki page Policies/Binary Compatibility Issues With C++ in the section Adding new data members to classes without d-pointer. I think the docu is wrong (please forgive and coreect me if i am wrong) the code sample following was taken from the docu and i have added some comment using triple slash (///):
Code: Select all
// BCI: Add a real d-pointer
Q_GLOBAL_STATIC(QHash<Foo *,FooPrivate>, d_func); ///FooPrivate is not declare as a pointer here.
static FooPrivate* d( const Foo* foo )
{
 // i think here it would failed since is expecting a pointer or?
    FooPrivate* ret = d_func()->value( foo, 0 );
    if ( ! ret ) {
        ret = new FooPrivate;///here is a pointer
        d_func()->insert( foo, ret );
    }
    return ret;
}
static void delete_d( const Foo* foo )
{
    FooPrivate* ret = d_func()->value( foo, 0 );
///delete is called without validating the pointer
    delete ret;
    d_func()->remove( foo );
}


The question is about the pointers usage here.

cafu
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
Given the nature of this problem, I would prefer to delegate to the people more versed in binary compatibility. Binary compatibility is very important to KDE.

Please send an email regarding this to kde-devel@kde.org.


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]
lamarque
Registered Member
Posts
152
Karma
2
OS
Code: Select all
// BCI: Add a real d-pointer
Q_GLOBAL_STATIC(QHash<Foo *,FooPrivate>, d_func); ///FooPrivate is not declare as a pointer here.

Lamarque: you're right, there is a "*" missing before FooPrivate. Just to be correct: FooPrivate is not being declared here, it is a template type, not a variable to be declared.

static FooPrivate* d( const Foo* foo )
{
 // i think here it would failed since is expecting a pointer or?

Lamarque: yes, this will not compile unless you fix the Q_GLOBAL_STATIC statement above.

    FooPrivate* ret = d_func()->value( foo, 0 );
    if ( ! ret ) {
        ret = new FooPrivate;///here is a pointer
        d_func()->insert( foo, ret );
    }
    return ret;
}
static void delete_d( const Foo* foo )
{
    FooPrivate* ret = d_func()->value( foo, 0 );
///delete is called without validating the pointer

Lamarque: well, there is no way to validate the pointer from here. Anyway, deleting a zeroed pointer is a harmless and if it is not zeroed then it will release the memory, which is what you want. You just must take care of not deleting the same pointe twice in a row, that corrupts memory.

    delete ret;
    d_func()->remove( foo );
}


Software engineer at Petrobrás http://www.petrobras.com.br/en/about-us/
KDE's Network Management maintainer


Bookmarks



Who is online

Registered users: Bing [Bot], Evergrowing, Google [Bot], rockscient