5 Replies - 198 Views - Last Post: 04 December 2017 - 04:07 PM Rate Topic: ***-- 2 Votes

#1 miralcefruit   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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);
		Circle.radius = data.at(2);
		shapes.push_back(shape);
	}
	else if(shape_name == "quad")
	{
		shape = new Quad;
		Quad.bottom_left = data.at(0);
		Quad.width = data.at(1);
		Quad.length = data.at(2);
		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   User is offline

  • D.I.C Head

Reputation: 33
  • View blog
  • 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

Was This Post Helpful? 1
  • +
  • -

#3 miralcefruit   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.

Was This Post Helpful? 0
  • +
  • -

#4 GazinAtCode   User is offline

  • D.I.C Head

Reputation: 33
  • View blog
  • 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.
Was This Post Helpful? 1
  • +
  • -

#5 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6216
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#6 miralcefruit   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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);
	}
	else if(shape_name == "quad")
	{
		shape = new Quad({data[0], data[1]}, data[2], data[3]);
		shapes.push_back(shape);
	}
	return shape;
}



Updated
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1