0

Sorry. i do not know how to ask this question, so i will give a example.

  1. Get the arg string from the command line Example: MyProgram.exe -logs=yes -console=no

    CmdLine CmdLine( ::GetCommandLine() );  // Get arg string.
    
  2. When i have the arg string. i wish to look for "logs" ("yes") and "console" get its value ("no").

    CmdLine.GetKey["logs"].GetValue();      // Get the value of "logs".
    CmdLine.GetKey["console"].AsBool();     // Get the value of "console".
    

I have to overload the [] operator ( void operator[] ( const std::string & str ); )

 GetKey["logs"]; // okay.

But i do not know how or if i can in C++ to do something like;

CmdLine.GetKey["console"].MyFunction();  // HOW to do this?

How can i tell "GetKey" to call "MyFunction(). (sorry for bad wording. but i do not know know what this is called.

Thank you for patients and help and ideas.

EDIT:

Sorry for confusion!

 CmdLine.GetKey["logs"].GetValue(); // Example!!!
  1. I overload the [] so i can look up "logs" in a std::map, std::unordered_map (whatever).

  2. When i find "logs" i wish for "GetKey["logs"]" to call "GetValue()" and return the value. I do know know what this is called when i do this;

    Func().SomeFunc().SomeOtherFunc();

Because i want to do the example i tried to explain.

Ariana_N
  • 1
  • 2

1 Answers1

0

Lets dissect it step by step:

CmdLine.GetKey["logs"].GetValue();
                     // call method on some object
              // call operator [] on GetKey
// GetKey is a member of CmdLine

As mentioned in a comment, GetKey is not a good name for a member. I suppose you don't really need it, but it was just your attempt to express that you are looking up the object on which you want to call GetValue() in CmdLine. I have to admit, I don't fully understand the example. Anyhow...

You can make operator[] return an object that has a member function that you can call:

#include <iostream>

struct foo {
    std::string value;    
    void print() { std::cout << value; }
};

struct bar {
    foo operator[](const std::string& x) {
        return {x};
    }
};

int main(int argc, char *argv[])
{
    bar b;
    b["hello world"].print();
}

Output:

hello world

For more on operator overloading read What are the basic rules and idioms for operator overloading?.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • Thank you for your help! I will read the link you post again. I also look at "function chaining" to see if this will help me. :) – Ariana_N Jul 07 '20 at 15:22