표준 벡터 형식의 클래스를 만들려고합니다. 나는 집합을 구현하고 벡터가 아닌 클래스를 가진 몇몇 프로그램을 작성해 왔기 때문에 다소 혼란 스럽다. 난 그냥C++ 벡터 클래스의 개인 데이터 멤버 호출
class Employee
{
private:
Struct Data
{
unsigned Identification_Number;
// Current capacity of the set
unsigned Department_Code;
// Department Code of employee
unsigned Salary;
// Salary of employee
str Name;
// Name of employee
}
내가 나중에 개인 데이터 멤버를 호출 할 경우를, 다음을 수행 할 수
여기 내 수업인가?
vector<Employee> Example;
//Assume there's data in Example
cout << Example[0].Identification_Number;
cout << Example[3].Salary;
그렇지 않으면 무엇이 적합한 컨테이너입니까? 이 데이터 집합을 처리 할 때 목록의 목록이 더 좋을까요? 당신이했습니다으로 Data
구조가이 경우에 완전히 불필요한 것을
class Employee
{
public:
unsigned GetID() const { return Identification_Number; }
unsigned GetDepartment() const { return Department_Code; }
unsigned GetSalary() const { return Salary; }
// Assuming you're using std::string for strings
const std::string& GetString() const { return string; }
private:
unsigned Identification_Number; // Current capacity of the set
unsigned Department_Code; // Department Code of employee
unsigned Salary; // Salary of employee
string Name; // Name of employee
};
참고 :
공개적으로 사용할 수있게하려면'구조체 '를 비공개로 만들어야하는 이유는 무엇입니까? 아니면'Struct Data {'a typo? – Cornstalks
그리고 왜 '데이터'구조가 첫 번째 위치에 있습니까? 'Data' 멤버들을 모두'Employee' 클래스에 직접 두는 것은 어떻습니까? –
여기의 컨테이너는 완전히 관련이 없으므로 개인 회원에게 모두 액세스하는 방법에 대해 혼란스러워하는 것 같습니다. 그 대답은 물론 당신이 가정하지 않은 것입니다; 그게 전부입니다. – GManNickG