Default constructor without parameters

Issue #30 resolved
Mario Guerra created an issue

May be it would be useful to include a default constructor without parameters, and to separate the initialization in the present constructor. This way it facilitates inheritance and delayed initialization. The code will look like:

NewPing.h

class NewPing {
    public:  
        NewPing();
        NewPing(uint8_t trigger_pin, .........
        void initialize(uint8_t trigger_pin, .........

NewPing.cpp

NewPing::NewPing() {
}
NewPing::NewPing(uint8_t trigger_pin, uint8_t echo_pin, unsigned int max_cm_distance) {
    initialize(trigger_pin, echo_pin, max_cm_distance);
}
NewPing::initialize(uint8_t trigger_pin, uint8_t echo_pin, unsigned int max_cm_distance) {
    // all the present stuff in constructor
}

Comments (10)

  1. Tim Eckel repo owner

    To what advantage? I don't understand what this does, how it would be implemented, or would provide.

  2. Mario Guerra reporter

    Hi Tim, it's only a proposal, and it's a small change, I already showed the changes to the code in the two files, and these changes doesn't affect the present interface. One thing is delayed initialization, for example first we create the NewPing object (or an array of objects) only calling the empty constructor. Later, when needed (and if needed), we can call the "initialize" procedure to actually configure the pins. Thanks for your work in the NewPing library.

  3. Tim Eckel repo owner

    Like I said, I don't understand what it does or why I would need it. I can't very well just include code if I don't know what it does to test it.

  4. Wim Matthijs

    ok, so I bumped into this one as well. I wanted to use a NewPing object inside my robot class. It was a pain because I couldn't declare an instance of NewPing in my class header. Somehow it mistakes the constructor with arguments for a definition of a function, apparently because you use brackets. so i made an empty default constructor in your lib and just swapped the empty NewPing object for a newly constructed one (with args) in the constructor of my class....

    Hope this makes things clearer why a default constructor could be useful. also the servo.h library which i used works in this way, constructor without args and attach info later.

    cheers

  5. Tim Eckel repo owner

    @wimmatthijs Nope, don't follow you in the slightest. But I'd be willing to look at a pull request.

  6. Wim Matthijs
    #include <NewPing.h>
    
    class MyRobot {
    private:
         const uint8_t aTripperPin = 4;
         const uint8_t anEchoPin = 5;
         const uint8_t anotherTripperPin = 6;
         const uint8_t anotherEchoPin = 7;
         const uint8_t maxDistance = 300;
    
    
    public: 
            NewPing aSensor(aTriggerPin, anEchopin, maxDistance);
            NewPing anotherSenror;
    }
    

    So for using your Lib in another class: aSensor wil not compile due to those brackets (); anotherSensor will compile. Hope you can follow this now, or just try this yourself ;-) Thanks for the lib though, after the adaptations it worked really well. I also adopted mine to respond with maxDistance instead of 0 in case of no return. Might include a Kalman filter later too. Let me know if any interest in that code to add to your lib.

    cheers!

  7. Tim Eckel repo owner

    @wimmatthijs Sorry, I don't follow what you're trying to do. It sounds like it wouldn't be backwards compatible. The problem with no response being max distance is that you don't know if there's something at the max distance, or nothing there at all, which is why it returns 0, which then allows the user to decide what to do.

  8. Wim Matthijs

    that is just side information, we were talking about the constructors here ;-) Hope that one was clear for you now :-) The reason i return max distance is because i work with children, for them its easier to grasp the idea this way. greetz, Wim

  9. Tim Eckel repo owner

    @wimmatthijs Sorry, should have broken that up in to two paragraphs. I still don't follow what you're trying to do with the constructor as it pertains to this issue. I'd be willing to look at a pull request as the descriptions I've been given I do not at all follow what you're trying to do. "Can't compile due to the braces"? Everything compiles just fine for me.

  10. Log in to comment