# Shape factory, dynamic allocation

Page 1 of 1

## 5 Replies - 198 Views - Last Post: 04 December 2017 - 04:07 PMRate Topic: 2 Votes //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=408023&amp;s=768a47871f6268100720ba284dd50bad&md5check=' + ipb.vars['secure_hash'], cur_rating: 3, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 miralcefruit

Reputation: 0
• Posts: 16
• Joined: 05-December 16

# Shape factory, dynamic allocation

Posted 04 December 2017 - 02:25 PM

```Shape* ShapeFactory::make_shape(const string &shape_name, const vector<double>&data)
{
Shape* = shape;
shape = nullptr;

if(shape_name == "circle")
{
shape = new Circle;
Circle.center.first = data.at(0);
Circle.center.second = data.at(1);
shapes.push_back(shape);
}
{
shapes.push_back(shape);
}
return shape;
}

```

Why is my syntax wrong here? Starting from Shape* = shape, all the way to Quad.length = data.at(2)
Here are the instructions:

Create a new Shape* variable named shape and set it equal to nullptr.
We will use this default of a nullptr to represent the case where we
receive an invalid shape_name (and thus cannot create a shape).
ii. Now we need to do some conditional checking:
1. if shape_name is equal to "circle"
i. set shape equal to a new Circle using a dynamic
allocation. You can use a similar brace-initialization to what
we had in class, but you will need to retrieve the center
coordinates and radius from the vector.
ii. Push shape back into the shapes vector.
2. else if shape_name is equal to "quad"
i. set shape equal to a new Quad using a dynamic allocation.
You can use a similar brace-initialization to what we had in
class, but you will need to retrieve the bottom-left
coordinates, length, and width from the vector.
ii. Push shape back into the shapes vector.
iii. return shape

Is This A Good Question/Topic? 0

## Replies To: Shape factory, dynamic allocation

### #2 GazinAtCode

Reputation: 33
• Posts: 121
• Joined: 26-September 16

## Re: Shape factory, dynamic allocation

Posted 04 December 2017 - 03:04 PM

Writing Shape* = shape; is like writing int = shape;, i.e., meaningless. You should have either something like
```Shape* shape; // declaration
shape = nullptr; // initialization
```
or
```Shape* shape = nullptr; // declaration with initialization
```

This post has been edited by GazinAtCode: 04 December 2017 - 05:31 PM

### #3 miralcefruit

Reputation: 0
• Posts: 16
• Joined: 05-December 16

## Re: Shape factory, dynamic allocation

Posted 04 December 2017 - 03:06 PM

Got it, thank you!

This post has been edited by Skydiver: 04 December 2017 - 03:51 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

### #4 GazinAtCode

Reputation: 33
• Posts: 121
• Joined: 26-September 16

## Re: Shape factory, dynamic allocation

Posted 04 December 2017 - 03:12 PM

The first method I gave you wasn't well thought out. It will most likely leave you with a "dangling" pointer and cause a memory leak. I've edited my previous post now.

### #5 Skydiver

• Code herder

Reputation: 6216
• Posts: 21,452
• Joined: 05-May 12

## Re: Shape factory, dynamic allocation

Posted 04 December 2017 - 03:52 PM

miralcefruit: There is no need to quote the post above yours. Just use the big Reply button or the Fast Reply area.

### #6 miralcefruit

Reputation: 0
• Posts: 16
• Joined: 05-December 16

## Re: Shape factory, dynamic allocation

Posted 04 December 2017 - 04:07 PM

```
Shape* ShapeFactory::make_shape(const string &shape_name, const vector<double>&data)
{
Shape* shape = nullptr;

if(shape_name == "circle")
{
shape = new Circle({data[0], data[1]}, data[2]);
shapes.push_back(shape);
}
{
shape = new Quad({data[0], data[1]}, data[2], data[3]);
shapes.push_back(shape);
}
return shape;
}

```

Updated