1

Consider the following class:

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 );

Question: Is there a way to overload/override/redefine the subscript operator s.t. one can, for example, have access to a vector of choice, among the three options.

I.e., suppose: SocialPrefNode ordering{ }. I want to be able to use the subscript operator as in ordering[ i ] AND be able to choose one, among the three vectors in the class, for the subscript/index i to act up on.

Example: one wants to access the third element in the preferences vector of a SocialPrefNode ordering. Then, one does ordering[ 2 ] and, thus, have access to the desired element.

2
  • So you have three data: the instance ordering, the subsript i, and... where is the type of container to search in? Commented Nov 17, 2018 at 18:12
  • 1
    You could write an operator[] that takes 0-2 and returns a std::vector<SocialPrefNode*> (either preferences or worsethan or indifference). Of course that would require a multi dimensional subscript like x[0][2] for element 2 in vector 0. Or you could have a wrapper type for the index so that the wrapper comes with the information which vector to read. Commented Nov 17, 2018 at 18:19

1 Answer 1

3

A possible solution is to use phantom types to wrap up the vector to use. I followed this blog post and came up with the following solution.

// Wrapper need once in the complete project:
template <typename T, typename Parameter>
class NamedType
{
public:
    explicit NamedType(T const& value) : value_(value) {}
    explicit NamedType(T&& value) : value_(std::move(value)) {}
    T& get() { return value_; }
    T const& get() const { return value_; }
private:
    T value_;
};

// Only showing the new parts in your class!
class SocialPrefNode {
public:
    // One phantom type for each array index
    using preferences_index = NamedType<std::ptrdiff_t, struct preferences_Parameter>;
    using worsethan_index = NamedType<std::ptrdiff_t, struct worsethan_Parameter>;
    using indifference_index = NamedType<std::ptrdiff_t, struct indifference_Parameter>;

    // One operator[] for each array index type
    SocialPrefNode* operator[](preferences_index i) { return preferences[i.get()]; }
    SocialPrefNode* operator[](worsethan_index i) { return worsethan[i.get()]; }
    SocialPrefNode* operator[](indifference_index i) { return indifference[i.get()]; }
}

// Usage is simple assuming a SocialPrefNode s.
s[0]; // This will not work!
s[SocialPrefNode::preferences_index{ 0 }];  // Accessing preferences
s[SocialPrefNode::worsethan_index{ 0 }];    // Accessing worsethan
s[SocialPrefNode::indifference_index{ 0 }]; // Accessing indifference

If you don't mind adding an external dependency, you can directly use the Github repo of the author of NamedType (where the class is a bit extended in comparison to here).

Sign up to request clarification or add additional context in comments.

1 Comment

I really liked your comment above, this really seems like the best way to do what I want to. Thanks for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.