Comment 14 for bug 1286611

Revision history for this message
Markus Elfring (elfring) wrote :

Does the following source code example become a bit more interesting after the addition of two pointer member variables?

> X=experiment.cpp && cat $X && g++ $X && echo '---' && ./a.out
#include <iostream>
#include <stdexcept>

struct ball
{
 char const* name;

 ball()
 {
  throw std::runtime_error("Surprise!");
 }

 ball(char const* tag) : name(tag)
 {
 }
};

class game
{
 ball *bounce, *jump;

public:
 game()
 {
  std::cout << "Game ball 1: " << static_cast<void*>(bounce) << std::endl;
  bounce = new ball("success");
  std::cout << "Game ball 2: " << static_cast<void*>(bounce) << std::endl;
  std::cout << "Game ball 3: " << static_cast<void*>(jump) << std::endl;
  jump = new ball;
  std::cout << "Game ball 4: " << static_cast<void*>(jump) << std::endl;
 }

 ~game()
 {
  delete jump;
  std::cout << "Bye" << std::endl;
  delete bounce;
 }

 void play(void)
 {
  std::cout << "Action!" << std::endl;
 }
};

int main(void)
{
 std::cout << "Test started ..." << std::endl;
 game x;
 x.play();
}
---
Test started ...
Game ball 1: 0x401190
Game ball 2: 0x1fd1010
Game ball 3: 0x400d70
terminate called after throwing an instance of 'std::runtime_error'
  what(): Surprise!

Would it be useful to release the object "bounce" correctly?