Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Every new should have a delete -- sometimes

29 views
Skip to first unread message

Paul

unread,
Nov 12, 2018, 5:45:36 AM11/12/18
to
Below is copy-pasted from http://ikeptwalking.com/bridge-design-pattern-explained/
Is there any reason why only two of the four new allocations are deleted?
I tried deleting all four and got no problems.

Thanks,

Paul


#include<iostream>
#include<string>

using namespace std;

class IColor
{
public:
virtual string Color() = 0;
};

class RedColor : public IColor
{
public:
string Color()
{
return "of Red Color";
}
};

class BlueColor : public IColor
{
public:
string Color()
{
return "of Blue Color";
}
};

class ICarModel
{
public:
virtual string WhatIsMyType() = 0;
};

class Model_A : public ICarModel
{
IColor* _myColor;
public:
Model_A(IColor *obj) :_myColor(obj){}
string WhatIsMyType()
{
return "I am a Model_A " + _myColor->Color();
}
};

class Model_B : public ICarModel
{
IColor* _myColor;
public:
Model_B(IColor *obj) :_myColor(obj){}
string WhatIsMyType()
{
return "I am a Model_B " + _myColor->Color();;
}
};

int main()
{
IColor* red = new RedColor();
IColor* blue = new BlueColor();

ICarModel* modelA = new Model_B(red);
ICarModel* modelB = new Model_A(blue);

cout << "\n" << modelA->WhatIsMyType();
cout << "\n" << modelB->WhatIsMyType() << "\n\n";

delete red;
delete blue;
return 1;
}

Paavo Helde

unread,
Nov 12, 2018, 8:27:16 AM11/12/18
to
On 12.11.2018 12:45, Paul wrote:
> Below is copy-pasted from http://ikeptwalking.com/bridge-design-pattern-explained/
> Is there any reason why only two of the four new allocations are deleted?
> I tried deleting all four and got no problems.

This is a fine example of why using 'new' and raw pointers is dangerous.
The allocations should look like something this instead:

std::unique_ptr<ICarModel> modelA = std::make_unique<Model_B>(red);


Mr Flibble

unread,
Nov 12, 2018, 12:08:38 PM11/12/18
to
The interface classes are missing virtual destructors.

/Flibble

--
“You won’t burn in hell. But be nice anyway.” – Ricky Gervais

“I see Atheists are fighting and killing each other again, over who
doesn’t believe in any God the most. Oh, no..wait.. that never happens.” –
Ricky Gervais

"Suppose it's all true, and you walk up to the pearly gates, and are
confronted by God," Bryne asked on his show The Meaning of Life. "What
will Stephen Fry say to him, her, or it?"
"I'd say, bone cancer in children? What's that about?" Fry replied.
"How dare you? How dare you create a world to which there is such misery
that is not our fault. It's not right, it's utterly, utterly evil."
"Why should I respect a capricious, mean-minded, stupid God who creates a
world that is so full of injustice and pain. That's what I would say."

woodb...@gmail.com

unread,
Nov 12, 2018, 4:52:14 PM11/12/18
to
I agree in that case, but would mention this:

https://stackoverflow.com/questions/38780596/how-to-handle-constructors-that-must-acquire-multiple-resources-in-an-exception#38780597

I use that approach here:
https://github.com/Ebenezer-group/onwards/blob/master/src/cmw/Buffer.hh

and find that compilers handle it better than a
unique_ptr based approach.


Brian
Ebenezer Enterprises - Enjoying programming again.
http://webEbenezer.net

0 new messages