C++ library inheritance

Hello,
I've a library containing an object which (among other initializations) have a function used to parse command line arguments.
Everytime it find an argument which can't be parsed by the default loop it calls a virtual function "parseOthers( int, int, char** )" which is supposed to be overridde by the derived classes in order to allow parsing of special argument in a per-software basis.
The problem is that the main class seems to call always the local method even when the children implements it (this is true even when the method is pure virtual).
I think that this could be my wrong interpretation of c++ scope when looking for methods implementations. Am I wrong? Is there any other way you suggest me for implementing the same function?

Here's an example code illustrating what I meant:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**** library.h ****/

class cmd_parser
{
public:
    cmd_parser( int argc, char**argv, bool auto_init = true );
    
    virtual int parseOthers( int curr, int argc, char **argv );
    
private:
    int _argc;
    char **_argv;
}

/**** library.cpp ****/

cmd_parser::cmd_parser( int argc, char**argv )
{
    _argc = argc;
    _argv = argv;
    
    if ( auto_init )
    {
        for ( int i = 0; i < argc; ++i )
        {
            switch (argv[i][0])
            {
            case 'a':
                //do something
                break;
            case 'b':
                //do other
                break;
            defaults:
                i = parseOthers( i, argc, argv );
                break;
            }
        }
    }
}

int cmd_parser::parseOthers ( int curr, int argc, char **argv )
{
    printf( "Unrecognized switch: %s", argv[curr] );
}

/************************** END OF LIBRARY ****************/

/* Project code */
/**** derived.h ****/
#include "library.h"

class derived_cmd_parser : public cmd_parser
{
    derived_cmd_parser( int argc, char **argv );
    
    int parseOthers ( int curr, int argc, char**argv );
}

/**** derived.cpp ****/

derived_cmd_parser::derived_cmd_parser( int argc, char **argv ) : cmd_parser(argc,argv)
{
    // do specific init
}

int derived_cmd_parser::parseOthers ( int curr, int argc, char **argv )
{
    switch( argv[curr][0] )
    {
        case 'f':
            if ( argc > curr+1 )
            {
                // do something with argv[curr] & argv[curr+1]
                curr++;
            }
            break;
    }
    return curr;
}

/**** main.cpp ****/

#include "derived.h"

int main ( int argc, char **argv )
{
    derived_cmd_parser *cmdline;

    cmdline = new derived_cmd_parser( argc, argv );
    
    // Here I'd like to have the "f" cmdline argument been parsed by the dervived_cmd_parser.
    // but by this code it generates to me 2 "Unrecognized ..." from the cmd_parser method
    
    delete cmdline;
    return 0;
}
Last edited on
First of all the code you have shared has syntax errors. I guess this would have happened while posting.

Otherwise it is correct. If you call it like this: cmdline->parseOthers(1,argc,argv); derived class function is called not the base class function. I tried it with correcting the errors, it was working.
For the errors, yes I quickly re-wrote the code for the post without any compilation sorry.
Regarding your answer, I know that calling it "directly" from the main works.
What I wish to know is if there's any way to make my code work as expected or to have the same behavior with a different approach, but keeping the same library philosophy...
What do you mean by "a different approach"?
I mean a different code which have the same idea (use a library which would parse the "standard" arguments and have a mechanism to parse the remaining ones in a per-softwae way). Re-parse the command line again would not be a suitable way since some options will use one argument while others will need two and I don't want to signal "errors" when there are none.

Thanks in advance for your help.
Topic archived. No new replies allowed.