简体   繁体   English

在 C++ 中使字符串变量成为类名

[英]Make a String Variable a Class Name in C++

I'm working on a program in C++ to make schedules for me, since I have a hard time focusing on school work.我正在用 C++ 编写一个程序来为我制定时间表,因为我很难专注于学校工作。 The code will make priorities of assignments based on the due dates, duration to do the assignments, etc.该代码将根据截止日期、完成任务的持续时间等确定任务的优先级。

Anyway, I've run into a massive problem.无论如何,我遇到了一个大问题。 I'm trying to assign a class variable's name to a variable!我正在尝试将类变量的名称分配给变量! For example:例如:

string a = "assignmentName" Class a(); //a is a variable and it's supposed to put the class's name as assignmentName

If you don't think this way will work, please tell me other solutions!如果您认为这种方式行不通,请告诉我其他解决方案!

Also, I'm still a beginner, so please try to explain a little simple!另外,我还是一个初学者,所以请尽量解释得简单一点!


Here's the code:这是代码:

#include <string>
#include <iostream>
using namespace std;

class Assignment
{
  private:
    int duration = 30; //Amount of time per assignment (in minutes)
    int due = 1; //Amount of days till its due, if it's one then the assignment is due in 1 days
    string name; //Name of assignment
  public:
    Assignment(int, int, string);
    ~Assignment();

    void setDuration(int duration) {this -> duration = duration;}
    void setDue(int due) {this -> due = due;}
    void setDue(int name) {this -> name = name;}

    int getDuration() { return duration; }
    int getDue() { return due; }
    string getName() { return name; }
};

Assignment::Assignment (int duration, int due, string name) //Constructor Method
{
  this -> duration = duration;
  this -> due = due;
  this -> name = name;
}

Assignment::~Assignment() //Destructor Method
{
  cout << "Assignment Object Destroyed" << endl;
}

int main()
{
  int assignmentAmount;
  int i;
  int userInput1;
  int userInput2;
  string userInput3;


  cout << "Enter assignment amount: ";
  cin >> assignmentAmount << "\n" << endl;

  for (i = 0; i < assignmentAmount; i++){
    cout << "Enter assignment" << i <<"'s " << "duration: ";
    cin >> userInput1 << endl;

    cout << "Enter assignment" << i << "'s " << "due date in days from now: ";
    cin >> userInput2 >> endl;

    cout << "Enter assignment" << i <<"'s " << "name: ";
    cin >> userInput3 >> endl;

    Assignment i(userInput1, userInput2, userInput3); //I is the class's number and it's supposed to be a variable
  }

  return 0;
}

What you are looking for is an array of Assignment objects, eg:您正在寻找的是一Assignment对象,例如:

int main()
{
  Assignment *assignments;
  int assignmentAmount;
  int userInput1;
  int userInput2;
  string userInput3;

  cout << "Enter assignment amount: ";
  cin >> assignmentAmount << "\n" << endl;

  assignments = new Assignment[assignmentAmount];

  for (int i = 0; i < assignmentAmount; i++){
    cout << "Enter assignment " << i << "'s duration: ";
    cin >> userInput1 << endl;

    cout << "Enter assignment " << i << "'s due date in days from now: ";
    cin >> userInput2 >> endl;

    cout << "Enter assignment " << i <<"'s name: ";
    cin >> userInput3 >> endl;

    // either:
    assignments[i] = Assignment(userInput1, userInput2, userInput3);

    // or:   
    assignments[i].setDuration(userInput1);
    assignments[i].setDue(userInput2);
    assignments[i].setName(userInput3);
  }

  // use assignments and assignmentAmount as needed...

  delete[] assignments;

  return 0;
}

Though, you should use std::vector instead of new[] directly:不过,您应该直接使用std::vector而不是new[]

#include <vector>
...

int main()
{
  std::vector<Assignment> assignment;
  int assignmentAmount;
  int userInput1;
  int userInput2;
  string userInput3;

  cout << "Enter assignment amount: ";
  cin >> assignmentAmount << "\n" << endl;

  for (int i = 0; i < assignmentAmount; i++){
    cout << "Enter assignment " << i << "'s duration: ";
    cin >> userInput1 << endl;

    cout << "Enter assignment " << i << "'s due date in days from now: ";
    cin >> userInput2 >> endl;

    cout << "Enter assignment " << i << "'s name: ";
    cin >> userInput3 >> endl;

    // either:
    assignments.push_back(Assignment(userInput1, userInput2, userInput3));

    // or:
    assignments.emplace_back(userInput1, userInput2, userInput3);
  }

  // use assignments as needed...

  return 0;
}

Or:或者:

#include <vector>
...

int main()
{
  std::vector<Assignment> assignment;
  int assignmentAmount;
  int userInput1;
  int userInput2;
  string userInput3;

  cout << "Enter assignment amount: ";
  cin >> assignmentAmount << "\n" << endl;

  assignments.resize(assignmentAmount);

  for (int i = 0; i < assignmentAmount; i++){
    cout << "Enter assignment " << i << "'s duration: ";
    cin >> userInput1 << endl;

    cout << "Enter assignment " << i << "'s due date in days from now: ";
    cin >> userInput2 >> endl;

    cout << "Enter assignment " << i << "'s name: ";
    cin >> userInput3 >> endl;

    // either:
    assignments[i] = Assignment(userInput1, userInput2, userInput3);

    // or:
    assignments[i].setDuration(userInput1);
    assignments[i].setDue(userInput2);
    assignments[i].setName(userInput3);
  }

  // use assignments as needed...

  return 0;
}

您无法将变量名作为变量获取,因为变量基本上就像数据的别名,当您的程序被编译时,所有这些名称不再存在。

The compiler does not emit symbol names to the executable (the debug version may have symbol names).编译器不会向可执行文件发出符号名称(调试版本可能有符号名称)。

If you want to associate or map symbol names to variables, you will need to declare the variable global and place the address into some kind of table.如果要将符号名称关联映射到变量,则需要将变量声明为 global 并将地址放入某种表中。

Example:例子:

int my_global_int_a;
std::map<std::string, int *> variable_dictionary;
variable_dictionary["my_global_int_a"] = &my_global_int_a;
// Here's how to get the variable
const std::string variable_name = "my_global_int_a";
int * p_variable = nullptr;
p_variable = variable_dictionary[variable_name];
std::cout << *p_variable << std::endl;

You may want to give deep thought to the need of retaining variable names.您可能需要深入考虑保留变量名称的必要性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM