Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Sprite copy constructor  (Read 6070 times)

0 Members and 1 Guest are viewing this topic.

Stupebrett

  • Newbie
  • *
  • Posts: 15
    • View Profile
Sprite copy constructor
« on: April 21, 2011, 08:26:12 pm »
Hello everyone! I've been using SFML for a few days, and when reading the tutorial about sprites, I came across this code snippet:

Code: [Select]
class MyPicture
{
public :

    // This is the copy constructor
    MyPicture(const MyPicture& Copy) : Image(Copy.Image), Sprite(Copy.Sprite)
    {
        // This is the trick : we setup the sprite
        // to use our image, instead of the one of Copy
        Sprite.SetImage(Image);
    }

    // ... a lot of useful functions ...

private :

    sf::Image  Image;
    sf::Sprite Sprite;
};


The problem is just that I have no idea how to use it, and the tutorial doesn't explain it very well. I'm not too good with classes either, but I know how to use them. Any help would be greatly appreciated. I would also really appreciate if someone could post an example code using this copy constructor. Maybe that would help me understand it a little bit more. Thanks.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Sprite copy constructor
« Reply #1 on: April 21, 2011, 08:53:21 pm »
Most of the copies are implicit (passing an argument, pushing an instance into a vector, ...). So you don't really have to "use" the copy constructor explicitely, it's just defined so that copying a MyPicture instance is well defined and works as expected, whenever it happens.

Here are examples where the copy constructor is invoked:
Code: [Select]
MyPicture p1;
MyPicture p1 = p2; // same as MyPicture p1(p2)

Code: [Select]
void func(MyPicture picture);

MyPicture p;
func(p);

Code: [Select]
std::vector<MyPicture> v;

MyPicture p;
v.push_back(p);
Laurent Gomila - SFML developer

 

anything