Giter Club home page Giter Club logo

Comments (3)

Tyill avatar Tyill commented on July 19, 2024 1

Hi.
Define a copy constructor and an assignment operator:

interpreter.h  

  explicit 
  Interpreter();
  ~Interpreter();

  Interpreter(const Interpreter&);
  Interpreter& operator=(const Interpreter&);
  .
  .
interpreter.cpp

  Interpreter::Interpreter() {
    m_d = new InterpreterImpl();
  }
  Interpreter::~Interpreter() {
    delete m_d;
  }
  Interpreter::Interpreter(const Interpreter& other) {
    m_d = new InterpreterImpl();
    *m_d = *other.m_d;
  }
  Interpreter& Interpreter::operator=(const Interpreter& other){
    if (this->m_d != other.m_d)   
      *m_d = *other.m_d;  
    return *this;
  }
  .
  .

And your example would be:

class Programmer {
public:
  Programmer() {
    cmdline.addOperator("=", [](string& leftOpd, string& rightOpd) ->string {
      leftOpd = rightOpd;
      return leftOpd;
    }, 10);
    
    cmdline.addFunction("summ", [](const vector<string>& args) ->string {
      string res;
      for (auto& v : args) {
        res += v;
      }
      return res;
    });
  }
  Interpreter cmdline;
};

class t_user {
public:
  std::string name = "root";
  Programmer programmer; // each user has own programmer / interpreter / environment    
};

std::vector<t_user> Users;

void main(void) {

  t_user  tmp_user;
  tmp_user.name = "user1";
  Users.push_back(tmp_user);  // add a t_user to Users

  // Users[0] we just pushed
  std::string input = "$a = summ(1, 2);";
  auto res = Users[0].programmer.cmdline.cmd(input);   // <------- complains here

  res; // 12
}

https://github.com/Tyill/interpreter/releases/tag/1.0.4

from interpreter.

hippyau avatar hippyau commented on July 19, 2024

Sorry, I ask because I can comment out....


  explicit 
  Interpreter();
  ~Interpreter();

//  Interpreter(const Interpreter&) = delete;   // <----- comment out compiles, but segfaults
  Interpreter& operator=(const Interpreter&) = delete;

if I comment out the delete line above from interpreter.h (because the compiler complained about deleted function) it compiles but segfaults on attempt to use.

I probably dumb in how I'm approaching

from interpreter.

hippyau avatar hippyau commented on July 19, 2024

Thanks so much, it makes sense now :) Thanks for your help.

from interpreter.

Related Issues (5)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.