I'm trying to write a class that will read and organize the command line arguments of a program.
Like, it will read -a as a 'switch', and --abcd as a 'switch' and you can define different switches as having parameters, so -a bc de fg will have bc and de as its parameters (that MUST go after -a if -a is defined as having two parameters) and fg would (if -a was only defined as having two parameters) be a standalone "statement"... anyway,
is it possible to overload the [] brackets in a class?
Say if -a had two switches, and there was an object aswitch for that switch...

cout<< aswitch; would output true if -a was set, false if otherwise,
cout<< aswitch[0]; would output the first parameter, and
cout<< aswitch[1]; would output the second.
right now I just have bool aswitch::isSet(); and char* aswitch::params(int);

Also, I don't know if i have the right understanding of "friend" functions...

class className{
public:
    friend int funcName();
protected:
    static char* varName;
};

int funcName(){
className varName = "newValue";
return 0;
}

making a "friend" function gives just a normal function outside a class access to that class's protected members right? Did I use it correctly in this example?

Dani AI

Generated

Yes — you can overload operator[] for a class. For a command-switch object it’s usually easiest to store parameters in a std::vector<std::string> and provide both const and non-const operator[] overloads so callers can read or mutate parameters. Decide whether operator[] should check bounds (throw std::out_of_range) or behave like standard containers (no check) and provide an at()-style accessor if you want checked access. See the C++ operator-overloading reference for details: operators on cppreference.

A compact implementation pattern looks like this:

#include <string>
#include <vector>
#include <iostream>
#include <stdexcept>

class ArgSwitch {
    bool set_;
    std::vector<std::string> params_;
public:
    ArgSwitch(bool s=false) : set_(s) {}
    void addParam(std::string p) { params_.push_back(std::move(p)); }
    std::size_t paramCount() const { return params_.size(); }

    const std::string& operator[](std::size_t i) const {
        if (i >= params_.size()) throw std::out_of_range("param index");
        return params_[i];
    }
    std::string& operator[](std::size_t i) {
        if (i >= params_.size()) throw std::out_of_range("param index");
        return params_[i];
    }

    explicit operator bool() const { return set_; }

    friend std::ostream& operator<<(std::ostream& os, ArgSwitch const& s) {
        return os << std::boolalpha << static_cast<bool>(s);
    }
};

About friend: friend gives a non-member function access to private/protected members, but it does not make that function a member. If you want a free function (for example operator<<) to print internal state or modify private static members, declare it friend inside the class and define it outside (or inline as above). The friend documentation is here: friend on cppreference.

Practical tips: prefer std::string/std::vector over raw char*, add paramCount() or size() for safety, and use explicit operator bool() rather than implicit conversions. If you need something robust immediately, consider existing parsers (getopt, Boost.Program_options) instead of rolling everything yourself. This addresses the operator[] idea you floated () and complements the linked thread pointed to.

Recommended Answers

All 2 Replies

ah, thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.