Analog methods in Go to C ++ how to implement?

type OutcomingDate struct { Time string } func (i *OutcomingDate) Packet(db *database.Database) (func(netes network.Netes), func(player *entitie.Player) []int) {...} type IncomingAuth struct { Key string Login string Password string MAC string IsCheat uint16 ClientVersion uint16 } func (i *IncomingAuth) Packet(db *database.Database) (func(netes network.Netes), func(player *entitie.Player) []int) {..} 
  • That is, how to write a function in c ++ that takes two functions as arguments and returns a structure? - KoVadim
  • Maybe so? void Packet (..., i * OutcomingDate) {} void Packet (..., i * IncomingAuth) {} Can you create a Packet class and use templates? How? - Tarakan
  • Explain in more detail what the methods in Go mean - just for C ++ 'nicks this is a dark forest ... Can it be in your own words - what do you need? - Harry
  • The syntax is probably messed up, but the meaning will be clear. Example: func (i * string) packet {fmt.Print (i) / * Print code * /} func (j * int) packet {{fmt.Print (j) / * Print size * /} func main () { str string = "432a" size int = 100 str.packet size.packet} Output: 432а 100 In general, I need to think about the architecture of the server using OOP. Write a Packet class to send packets (of different structure). The example, originally written, uses Packet functions that accept various methods. - Tarakan

1 answer 1

I know Go, and quite a bit of C ++. Sketched something similar to what you have:

 #include <cinttypes> #include <functional> #include <string> #include <vector> struct MethodReturn { std::function<void (void)> f1; std::function<std::vector<int> (void)> f2; }; struct OutcomingDate { std::string time; struct MethodReturn Packet(); }; struct IncomingAuth { std::string key; std::string login; std::string password; std::string mac; uint16_t is_cheat; uint16_t client_version; }; MethodReturn OutcomingDate::Packet() { auto f1 = []() {}; auto f2 = []() -> std::vector<int> { std::vector<int> a; return a; }; MethodReturn mr = {f1, f2}; return mr; } 

-std=c++11 with -std=c++11 . If something is wrong, I hope people who know more will write a better answer.

  • You obviously were too modest about знаю совсем немного C++ : D Why write a struct before MethodReturn ? - gil9red 4:41
  • @ gil9red Thanks :) I thought that without a struct would not compile, since there is no typedef struct {/*...*/} MethodReturn , but apparently wrong. Removed. - Ainar-G
  • And without him, it will come together :) You can remove it from OutcomingDate :: Packet I was a little embarrassed by its presence in declarations of variables and methods :) - gil9red
  • one
    Sorry stupid in Go, but even in your C ++ code I can’t understand what the most important task is? For example, where does struct IncomingAuth play? I see a function returning a structure with two called fields, but I can’t think why? Where to apply it? Sorry again, but just interesting ... - Harry
  • @Harry I just rewrote Go'sh C ++ code. Since it is impossible to return several values ​​from a function in C ++, I put them in a structure. Why do so - ask OP'a. - Ainar-G