开发者

Persistent pointer to parent

How can I use this as a persistent pointer, so it would work outside of the current scope?

As for this example, I don't know how should I set the fx.parent:

class Effect
{
    Card* parent;
};

class Card
{
    vector<Effect> effects;
    void addEffect(Effect);
};

Card::addEffect(Effect fx)
{
    /*
     * the `this` pointer is not persistent and
     * will not work outside of this scope
     */
    fx.parent = this; 
    this->effects.push_back(fx);
}

PS: I'd be grateful for any literature about when pointers get destroyed, 开发者_StackOverflowinvalidated, etc. I could not really find anything readable. Nothing at all, actually.


Pushing elements into containers copies them. So you should link the copy, not the original:

Card::addEffect(Effect fx)
{
    effects.push_back(fx);
    effects.back().parent = this;
}

Otherwise, you will link to a local variable that goes out of scope when addEffect returns.

Note that this code generates another copy because of pass-by-value. Let's get rid of that:

Card::addEffect(Effect const& fx)
{
    effects.push_back(fx);
    effects.back().parent = this;
}

Also note that as soon as the capacity is exhausted, the vector will do an internal reallocation, and all pointers will become invalid. The easiest workaround is to reserve enough space from the beginning:

Card::Card()
{
    effects.reserve(1000);   // never more than 1000 effects at once
}

If that is unacceptable to you, you must either use a different container (std::list<Effect>) or put your effects on the heap and manage them manually (std::vector<Effect*>) or with a smart pointer (std::vector<boost::shared_ptr<Effect> >).


The this pointer of an object is the same as taking the address:

#inclide <iostream>

class X{
public:
    X* GetThis(){
        return this;
    }
};

int main(){
    X x;
    X* addr_x = &x;
    X* this_x = x.GetThis();
    if(addr_x == this_x)
        std::cout << "Both are the same" << std::endl;
    else
        std::cout << "Shouldn't happen" << std::endl;
}

See at Ideone. As such, this is just a normal pointer to your class with a special name, so there's no problem with your code regarding the use of this. Aside from that, there are some errors though, like the missing return type in the definition of Card::addEffect and Card* parent in class Effect being private and as such it cannot be accessed by Card.


Ok, so it goes like this:

First of all the keyword "this" means the current object.
So in this context:

Card::addEffect(Effect fx)
{
    fx.parent = this; 
    this->effects.push_back(fx);
}

this is the Card object in which the addEffect() function was called..

Second, pointers don't get invalidated or destroyed. You can destroy the object they are pointing at by using the delete operator:

Blah* pointer = new Blah();
delete pointer;


The way you keep track of your card object can also cause problems. When a copy is made of your card object the parent pointers will keep pointing to the original Card object. You will need to write a copy constructor to solve this issue or prevent copying from happening.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜