0

I have written a class for a curve in the complex plane in c++. It looks like this,

#ifndef CURVE_H
#define CURVE_H

#include <iostream>

#include "complex_numbers.h"

class Curve {
    double min;
    double max;
    Complex(*f)(double);
public:
    Curve(Complex(*g)(double), double min_t, double max_t) {
        f = g;
        min = min_t;
        max = max_t;
        if (min > max) {
            std::cout << "Domain error." << std::endl;
        }
    }

    double get_min();

    double get_max();

    Complex at(double t);
};

#endif

This also relies on another class I have written (Complex) which is just a simple class for the complex numbers.

If a curve is defined on the interval [a,b], I don't want a curve with b<a to be able to be defined. Can I prevent this in my constructor somehow? As of right now I'm printing an error message but I can still define a curve (f,1,0) for example. Can I cancel the initialization in some way, if min_t > max_t?

  • 1
    `throw` exception? – Jarod42 Oct 27 '21 at 10:34
  • 1
    Off-topic: There's already `std::complex` in header `` – you might consider if it matches your needs as well and possibly replace your custom class (no need to re-invent the wheel). – Aconcagua Oct 27 '21 at 10:35
  • @spinosarus123 Just swap them. – Vlad from Moscow Oct 27 '21 at 10:35
  • Off-topic 2: You should get used to implementing the constructor's initialiser list (not to be confused with `std::initializer_list`): `Curve(/*...*/) : f(g), min(min_t), max(max_t) { /*still can throw here*/ }` – this prefers direct initialisation by value over default initialisation + assignment, which can be especially relevant with complex types. Additionally some types *only* can be initialised that way (references, `const` members, non-default-constructible types, ...). – Aconcagua Oct 27 '21 at 10:42
  • @VladfromMoscow Hm... Thought about that as well, discarded, though, as it abets erroneous usage of the class. Passing min as 12 and receiving 10 from getter afterwards maybe is a bit odd, isn't it? – Aconcagua Oct 27 '21 at 10:46
  • As an addendum... you can leverage the type system `class CurveMin { explicit CurveMin(double); };` - If the `Curve` c'tor accepts named types instead of doubles, a caller must write `CurveMin(1), CurveMax(0)` explicitly during initialization. This should make them go "wait a minute..." – StoryTeller - Unslander Monica Oct 27 '21 at 11:03
  • @StoryTeller-UnslanderMonica Nice from the *'wait a minute'* point of view – not that nice if I have to create large numbers of these structs... – Aconcagua Oct 27 '21 at 11:29
  • @Aconcagua - Today I learned that 2 is a "large number". If your c'tors take more than 3-4 arguments, you have other problems. – StoryTeller - Unslander Monica Oct 27 '21 at 11:33
  • @StoryTeller-UnslanderMonica Oh, I meant a large number of `Curve`s (not in a loop, of course...). Having to write `CurveM[in|ax]` all the time gets a bit uncomfortable then. – Aconcagua Oct 27 '21 at 11:46

0 Answers0