Including an assert into the constructor of a class via if

Greetings everyone.
I'm working on some stuff for university right now and I've stumbled over a problem I can't figure out right now, but I'm pretty sure one of you guys may. Basic idea of my code is that I can create certain shapes in 3D-space as shape is the parent class and all others are children of shape.
One of them is the box.

Now the box gets defined by two points named min and max (which isn't actually true, they're not always min and max, but well) and of course, to define a box by two points only, it is required that my points are actually the edge points.
My point3d class has x, y and z points as member variables which can be called via [ ]-operator which I implemented earlier.

What I'm asking is: To avoid the creation of a box that is invalid (for example because the points are (1,1,1) and (1,1,0) I need to implement a test if the given points can create a box. I would like do so in the constructor.
I could, of course do it later but I don't want to.

My question now is: How do I insert an if case into my constructor?
Right now I have no idea. I've written the following constructor:

1
2
3
4
5
6
box::box(point3d const& min, point3d const& max, std::string const& name, color const& col):
shape(name, col),
m_min(min),
m_max(max)

{std::cout<<"box constructor used"<<std::endl;}


And my if case should be somehow like this:

1
2
3
4
5
6
7
8
if (min[0] == max[0] && min[1] == max[1]){
    assert(0);}

if (min[0] == max[0] && min[2] == max[2]){
    assert(0);}

if (min[1] == min[1] && min[2] == max[2]){
    assert(0);}


Or similar, please do not care wether this if does kick out all boxes that should be kicked out or not. This is not topic of my question.
All necessary operators are written.
min[0] returns the x, [1] the y and [2] the z value of the point3d.

Thanks for your help!
Last edited on
closed account (o1vk4iN6)

Just add the code into the constructor, you also don't need those if statements just put the condition into the assert:

1
2
3
4
5
6
7
8
9

box::box(point3d const& min, point3d const& max, std::string const& name, color const& col)
     : shape(name, col), m_min(min), m_max(max)
{
     assert( (max[0] - min[0]) > 0 );

     /* ... */
}
Last edited on
Thanks alot, it works.
Was as simple as I though.
Already had it like this but had a typing mistake in it I didn't recognize.

Thanks!
Topic archived. No new replies allowed.