I am trying to overload << operator to seinding the command

Hi I have a bachelor project about DJI Tello Drone and I wanted to overload this "<<" operator so that I can call the function like this:

1
2
3
4
5
int main(){
Tello drone;
drone<<command();
return 0;
}


and not like this:
1
2
3
4
int main(){
drone.command();
return 0;
}


The Tello is a class and here is the definition:
1
2
3
4
5
6
7
8
9
10
class Tello
{
public:
    tello();
    void Command();
    tello operator<<(string(*command)(void));
private:
        string command;
	void SendCommand(const string&);
};


1
2
3
4
5
6
7
8
9
10
11
tello::tello(){}
void tello::Command()
{
	command = "command";
}
tello tello::operator<<(string(*command)(void))
{
	SendCommand();
	return tello();
}


Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>

struct tello {

    using command_t = std::string() ; // nullary function returning std::string

    explicit tello( command_t* command = nullptr ) noexcept : command(command) {}

    tello& operator<< ( command_t* cmd ) noexcept { command = cmd ; return *this ; }

    // execute the command and return the result
    std::string exec() const { return command == nullptr ? "" : command() ; }

    command_t* command ; // pointer to function
};

std::string test_cmd() { return "this is a test!" ; }

int main() {

    tello drone ; // drone.command == nullptr
    drone << test_cmd ; // drone.command == &test_cmd
    std::cout << drone.exec() << '\n' ; // this is a test!
}

http://coliru.stacked-crooked.com/a/0851e8a9e06e4b96
If the function is unary (takes an argument):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>

struct tello {

    using command_t = std::string( const std::string& ) ; // unary function returning std::string

    explicit tello( command_t* command = nullptr ) noexcept : command(command) {}

    tello& operator<< ( command_t* cmd ) noexcept { command = cmd ; return *this ; }

    // execute the command and return the result
    std::string exec() const { return command == nullptr ? "" : command(arg) ; }

    command_t* command ; // pointer to function
    std::string arg = "a_test_arg" ;
};

std::string test_cmd( const std::string& arg ) { return "test_cmd(" + arg + ")\n" ; }

int main() {

    tello drone ; // drone.command == nullptr
    drone << test_cmd ; // drone.command == &test_cmd
    std::cout << drone.exec() << '\n' ; // test_cmd(a_test_arg)
}

http://coliru.stacked-crooked.com/a/a8b287ec6fdb7eb9
I am actually using function pointer or std function...
Also how do i write code so i can invoke functions like this:

1
2
3
4
5
6
7
8

int main()
{
     tello drone;
     drone << command() << delay(2) << takeoff();
return 0;
}
Not sure what you are aiming for.
To be able to write: drone << command() << delay(2) << take_off();

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <functional>
#include <string>

struct tello
{
    // helper types
    struct command_ { std::function< std::string() > cmd ; } ;
    struct delay_ { int secs ; } ;
    struct take_off_ { std::function< void() > fn ; };

    // these are just some trivial examples of the overloads
    tello& operator<< ( const command_& c ) { std::cout << "command() => " + c.cmd() + '\n' ; return *this ; }
    tello& operator<< ( const delay_& d ) { std::cout << "delay(" << d.secs << ")\n" ; return *this ; }
    tello& operator<< ( const take_off_& t ) { std::cout << "take_off() => " ; t.fn() ; return *this ; }
};

// factory functions to create the helper types
template < typename FN > tello::command_ command( FN fn ) { return {fn} ; }
tello::command_ command() { return command( [] { return "this is the default command!" ; } ) ; } // default command

tello::delay_ delay( int secs = 2 ) { return {secs} ; } // default delay == 2 secs

template < typename FN > tello::take_off_ take_off( FN fn ) { return {fn} ; }
tello::take_off_ take_off() { return take_off( [] { std::cout << "default take off function!\n" ; } ) ; } // default

int main()
{
    tello drone ;
    drone << command() << delay(23) << take_off() ; // use defaults for command, take_off
}

http://coliru.stacked-crooked.com/a/a3a114bcea4377f1
Topic archived. No new replies allowed.