C
C
CodeInside2017-02-13 14:24:27
OOP
CodeInside, 2017-02-13 14:24:27

How to implement static field/method inheritance if possible?

I am writing a Linux terminal emulator (like Bash) for a coursework. It turns out I created an abstract base class Command.

class Command
{
public:
  Command(const std::string&);

  std::vector<std::string> getArguments();
  std::vector<std::string> getOptions();

  virtual void execute(Directory& currentDirectory) = 0;
  virtual  std::string getCommandName() = 0;

protected:
  std::vector<std::string> arguments_;
  std::vector<std::string> options_;

  void parseStr(std::string);
};

Working commands are already inherited from it, which implement the execute method and must somehow implement their name (example: cp, cd, ls, pwd). The problem is that I don’t know how to correctly implement the fact that each command should have its own name. It should naturally be static. I think that in C#/Java this is implemented through interfaces, but there are none in C++. You may have already noticed, I decided to implement this through the getCommandName method, which derived classes are required to implement. But in this case, in order to get the name of the command, it is necessary to create its object, because a virtual method cannot be static. I need this in order to implement such an algorithm:
  1. I read the string entered by the user.
  2. I do split on spaces (take into account brackets, etc.) and due to this I get the name of the command.
  3. I create a pointer to Command (the base abstract class).
  4. I throw the names of commands into commands into a certain container (stack, vector, I don’t care, but I implement it through a vector). This is where the problem arose, because you don't want to create an instance of each class.
  5. I compare the received name of the command with the names in the classes.
  6. I create an object of the corresponding class and write down its address in the pointer to the Command.
  7. It turns out such a beautiful record, for which I suffer so much command->execute(/* Current directory.*/)

What do you advise?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mercury13, 2017-02-13
@CodeInside

First. Explain, for yourself and for me, what is a Command object?
My vision is to separate the Command objects (the user-entered and parsed string) and the Program (the program that implements the command). I also drew - if you want, use it, if you don't want it - the Console object (I / O console) and System (program environment like the current directory, environment variables, file system).
I'm working with values ​​and pointers here, in C++03 terms, but you might be interested in C++11 smart pointers.

std::string commandLine = console.getSomeCommandLine();
Command command;
std::string error;
if (!command.parse(commandLine, error)) {
  console.err().writeln(error);
  return;
}
Program* program = system.findProgram(command.programName);
if (!program) {
  console.err().writeln("Bad command or file name");
  return;
}
Console redirectedConsole = console.redirectStreams(command);
program->exec(redirectedConsole, system, command.getArguments());

Second. Return links, it's faster.
const std::vector<std::string>& getArguments() const;
const std::vector<std::string>& getOptions() const;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question