Is this code from sourcemaking.com bugged?

P

pauldepstein

I was a bit surprised to see this on sourcemaking.com because it
generally appears excellent. My opinion is that this code is faulty
because the destructor has not been made virtual in the base class
Stooge. Am I correct, or is there some reason a non-virtual
destructor is ok?

Thanks a lot,

Paul Epstein

Code is pasted below:

class Stooge
{
public:
// Factory Method
static Stooge *make_stooge(int choice);
virtual void slap_stick() = 0;
};

int main()
{
vector<Stooge*> roles;
int choice;
while (true)
{
cout << "Larry(1) Moe(2) Curly(3) Go(0): ";
cin >> choice;
if (choice == 0)
break;
roles.push_back(Stooge::make_stooge(choice));
}
for (int i = 0; i < roles.size(); i++)
roles->slap_stick();
for (int i = 0; i < roles.size(); i++)
delete roles;
}

class Larry: public Stooge
{
public:
void slap_stick()
{
cout << "Larry: poke eyes\n";
}
};
class Moe: public Stooge
{
public:
void slap_stick()
{
cout << "Moe: slap head\n";
}
};
class Curly: public Stooge
{
public:
void slap_stick()
{
cout << "Curly: suffer abuse\n";
}
};

Stooge *Stooge::make_stooge(int choice)
{
if (choice == 1)
return new Larry;
else if (choice == 2)
return new Moe;
else
return new Curly;
}
 
A

Alf P. Steinbach

* (e-mail address removed):
I was a bit surprised to see this on sourcemaking.com because it
generally appears excellent. My opinion is that this code is faulty
because the destructor has not been made virtual in the base class
Stooge. Am I correct, or is there some reason a non-virtual
destructor is ok?

Thanks a lot,

Paul Epstein

Code is pasted below:

class Stooge
{
public:
// Factory Method
static Stooge *make_stooge(int choice);
virtual void slap_stick() = 0;
};

int main()
{
vector<Stooge*> roles;
int choice;
while (true)
{
cout << "Larry(1) Moe(2) Curly(3) Go(0): ";
cin >> choice;
if (choice == 0)
break;
roles.push_back(Stooge::make_stooge(choice));
}
for (int i = 0; i < roles.size(); i++)
roles->slap_stick();
for (int i = 0; i < roles.size(); i++)
delete roles;
}

class Larry: public Stooge
{
public:
void slap_stick()
{
cout << "Larry: poke eyes\n";
}
};
class Moe: public Stooge
{
public:
void slap_stick()
{
cout << "Moe: slap head\n";
}
};
class Curly: public Stooge
{
public:
void slap_stick()
{
cout << "Curly: suffer abuse\n";
}
};

Stooge *Stooge::make_stooge(int choice)
{
if (choice == 1)
return new Larry;
else if (choice == 2)
return new Moe;
else
return new Curly;
}


UB.

Cheers, & hth.,

- Alf
 
P

pauldepstein

* (e-mail address removed):


I was a bit surprised to see this on sourcemaking.com because it
generally appears excellent.  My opinion is that this code is faulty
because the destructor has not been made virtual in the base class
Stooge.  Am I correct, or is there some reason a non-virtual
destructor is ok?
Thanks a lot,
Paul Epstein
Code is pasted below:
class Stooge
{
  public:
    // Factory Method
    static Stooge *make_stooge(int choice);
    virtual void slap_stick() = 0;
};
int main()
{
  vector<Stooge*> roles;
  int choice;
  while (true)
  {
    cout << "Larry(1) Moe(2) Curly(3) Go(0): ";
    cin >> choice;
    if (choice == 0)
      break;
    roles.push_back(Stooge::make_stooge(choice));
  }
  for (int i = 0; i < roles.size(); i++)
    roles->slap_stick();
  for (int i = 0; i < roles.size(); i++)
    delete roles;
}

class Larry: public Stooge
{
  public:
    void slap_stick()
    {
        cout << "Larry: poke eyes\n";
    }
};
class Moe: public Stooge
{
  public:
    void slap_stick()
    {
        cout << "Moe: slap head\n";
    }
};
class Curly: public Stooge
{
  public:
    void slap_stick()
    {
        cout << "Curly: suffer abuse\n";
    }
};
Stooge *Stooge::make_stooge(int choice)
{
  if (choice == 1)
    return new Larry;
  else if (choice == 2)
    return new Moe;
  else
    return new Curly;
}

UB.

Cheers, & hth.,

- Alf


Actually, I've spotted very many typos there since my posting so I
shouldn't have been surprised. But nevertheless, it's the best free
resource on C++ Design patterns that I've seen.

Paul Epstein
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,160
Messages
2,570,889
Members
47,420
Latest member
ZitaVos505

Latest Threads

Top