1

次のクラスを検討してください。

class SocialPrefNode{

public:

// Constructors & Destructor
SocialPrefNode( );
SocialPrefNode( char self, int ind, int link, bool stack, std::vector<SocialPrefNode*> pref,
                std::vector<SocialPrefNode*> worse, std::vector<SocialPrefNode*> indiff );

SocialPrefNode( const SocialPrefNode& copy );

~SocialPrefNode( );

// Setters
void set_id( char self );

void set_index( int ind );
void set_lowlink( int link );

void set_onstack( bool stack );

void set_pref( std::vector<SocialPrefNode*> prefs );
void set_pref( SocialPrefNode& prefs );

void set_worse( std::vector<SocialPrefNode*> wrs );
void set_worse( SocialPrefNode& wrs );

void set_indiff( std::vector<SocialPrefNode*> indiff );
void set_indiff( SocialPrefNode& indiff );

// Getters
char get_id( ){ return id; }

int get_index( ){ return index; }
int get_lowlink( ){ return lowlink; }

bool get_onstack( ){ return onstack; }

std::vector<SocialPrefNode*> get_preferences( ){ return preferences; }
std::vector<SocialPrefNode*> get_worse( ){ return worsethan; }
std::vector<SocialPrefNode*> get_indiff( ){ return indifference; }

// Operators
SocialPrefNode& operator=( const SocialPrefNode& copy );

private:

char id{ };

int index{ };
int lowlink{ };

bool onstack{ };

std::vector<SocialPrefNode*> preferences{ };
std::vector<SocialPrefNode*> worsethan{ };
std::vector<SocialPrefNode*> indifference{ };
};

std::ostream& operator<<( std::ostream& os, SocialPrefNode& node );

質問:添字演算子 st をオーバーロード/オーバーライド/再定義する方法はありますか? たとえば、3 つのオプションの中から選択したベクトルにアクセスできます。

つまり、次のように仮定しますSocialPrefNode ordering{ }。添え字演算子をordering[ i ] ANDのように使用して、クラス内の 3 つのベクトルから 1 つを選択して、添え字/インデックス i を動作させたいと考えています。

preferences例: a のベクトルの3 番目の要素にアクセスしたいとしますSocialPrefNode ordering。次に、ordering[ 2 ]必要な要素にアクセスできるようになります。

4

1 に答える 1