Chivos
Chivos

Reputation: 83

Pointer to pointer is causing crash

Sample Code

sf::Drawable *mDrawables = new  sf::Shape(sf::Shape::Circle(-10, -10,5,sf::Color::Green));
Particle particle(mDrawables);


Particle::Particle(sf::Drawable *hum)
{
    *mDrawables = *hum;
}

From the narrowing down I have done, I figure the crash is happening somewhere in here. Once the constructor is called, the program crashes. Does anyone know what I am doing wrong here? Any help would be greatly appreciated.

Upvotes: 0

Views: 96

Answers (2)

Tom
Tom

Reputation: 2389

It looks like you're over-writing the contents of mDrawables with the contents of mDrawables (aliased as the parameter hum in the Particle constructor).

The line *mDrawables = *hum should be read as "assign the contents of hum to the contents of mDrawables".

It's hard to tell why this might cause a crash; there's not enough details in the code sample (I don't know what a Shape does when you copy it).

I think you might need to re-structure the design here, but I can't tell what your overall intent is, so I can't give you any good advice in that regard.

Upvotes: 0

Yola
Yola

Reputation: 19043

I don't see pointer to pointer?

*mDrawables = *hum;

mDrawables is a pointer, but *mDrawables dereferenced pointer and may be class is not copyable.

May be you should try this

mDrawables = hum;

Also i hope what sf::Shape inherited from sf::Drawable

Upvotes: 1

Related Questions