C++:如何在类中获取带有动态变量的私有属性?
C++: How to get a private property with a dynamic variable in a class?
我想创建一个简单的 Car
class,它有一个 Car::get
方法,我可以使用该方法通过以下字符串访问汽车的私有属性:
// Create Car Intance
Car myCar;
cout << myCar.get("wheels");
我的问题是我不知道如何用动态变量指向私有属性。这是 class:
// Libraries & Config
#include <iostream>
using namespace std;
// Create Car Class
class Car {
private:
int wheels = 4;
int doors = 5;
public:
Car(); // constructor
~Car(); // deconstructor
int get(string what); //
};
// Constructor
Car::Car(){ cout << "Car constructed." << endl; }
// Deconstructor
Car::~Car(){ cout << "Car deconstructed." << endl; }
// Get Method
int Car::get(string what){
// === THE PROBLEM ===
// How do I access the `wheels` property of the car class with the what argument?
return this[what] // ??
}
// Create Car Instance
Car myCar;
cout << myCar.get("wheels");
class Car {
private:
int wheels = 4; <<< This would flag an error as you cannot provide
int doors = 5; <<< in class initialization for non-consts.
int Car::get (string what)
{
if( what == "wheels" ) //// check for case sensitivity...
return wheels;
else
return doors;
}
您可以使用 std::map
:
#include <map>
#include <string>
#include <iostream>
using namespace std;
class Car {
private:
std::map<std::string, int> parts = {{"wheels", 4}, {"doors", 5}};
public:
Car();
~Car();
int get(std::string what);
};
// Constructor
Car::Car(){ std::cout << "Car constructed." << endl; }
// Deconstructor
Car::~Car(){ std::cout << "Car deconstructed." << endl; }
// Get Method
int Car::get(string what){
return parts[what];
}
int main()
{
// Create Car Intance
Car myCar;
cout << myCar.get("wheels") << '\n';
}
std::map
的工作原理值得一读:http://en.cppreference.com/w/cpp/container/map
我想创建一个简单的 Car
class,它有一个 Car::get
方法,我可以使用该方法通过以下字符串访问汽车的私有属性:
// Create Car Intance
Car myCar;
cout << myCar.get("wheels");
我的问题是我不知道如何用动态变量指向私有属性。这是 class:
// Libraries & Config
#include <iostream>
using namespace std;
// Create Car Class
class Car {
private:
int wheels = 4;
int doors = 5;
public:
Car(); // constructor
~Car(); // deconstructor
int get(string what); //
};
// Constructor
Car::Car(){ cout << "Car constructed." << endl; }
// Deconstructor
Car::~Car(){ cout << "Car deconstructed." << endl; }
// Get Method
int Car::get(string what){
// === THE PROBLEM ===
// How do I access the `wheels` property of the car class with the what argument?
return this[what] // ??
}
// Create Car Instance
Car myCar;
cout << myCar.get("wheels");
class Car {
private:
int wheels = 4; <<< This would flag an error as you cannot provide
int doors = 5; <<< in class initialization for non-consts.
int Car::get (string what)
{
if( what == "wheels" ) //// check for case sensitivity...
return wheels;
else
return doors;
}
您可以使用 std::map
:
#include <map>
#include <string>
#include <iostream>
using namespace std;
class Car {
private:
std::map<std::string, int> parts = {{"wheels", 4}, {"doors", 5}};
public:
Car();
~Car();
int get(std::string what);
};
// Constructor
Car::Car(){ std::cout << "Car constructed." << endl; }
// Deconstructor
Car::~Car(){ std::cout << "Car deconstructed." << endl; }
// Get Method
int Car::get(string what){
return parts[what];
}
int main()
{
// Create Car Intance
Car myCar;
cout << myCar.get("wheels") << '\n';
}
std::map
的工作原理值得一读:http://en.cppreference.com/w/cpp/container/map