Inheritance Issue, Accessing Parent Fucntion Within Nested Class

Below is my class where I'm inheriting a class that has a nested class within it. The CMethod0173 is basically duplicating CMethod172, and is one of my first attempts at utilizing the re-usability aspects of inheriting classes. I want to override the parent's CMethod172::Args::getArgs(AlignArgs*args); function by having it call the parent class and then do it's own thing. Below that are some things I tried that didn't work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef CMETHOD0173_H
#define	CMETHOD0173_H
#include"CMethod0172.h"
class CMethod172;
class CMethod0173 : public CMethod172
{
public:
    CMethod0173(AlignArgs *args);
    class Args
    {
    public:
        void getArgs(AlignArgs *args);
    };
private:
};
#endif	/* CMETHOD0173_H */ 


Try 1:
1
2
3
4
5
#include "CMethod0173.h"
void CMethod0173::Args::getArgs(AlignArgs *args)
{
    CMethod172::Args::getArgs(args);
}

This didn't work because it has to be an instance variable. I suppose I could fix this with a static function if it didn't need to be an instance because of inherited data. I'm not showing the inherited class for simplicity because it's referencing hundreds of classes that I would have to show for it to make any sense. Anyway, it has data in it that it tracks throughout each instance, so static isn't really an option for the re-usability concept because I would then have to go re-write it to all track things somewhere else. Anyway, I tried this next:

Try 2:
1
2
3
4
5
#include "CMethod0173.h"
void CMethod0173::Args::getArgs(AlignArgs *args)
{
    this->CMethod172::Args::getArgs(args);
}

This didn't work because of the error that the qualified name "CMethod172::Args::getArgs(args);" is not a member of class "CMethod0173::Args" or its base classes. I hate to admit that I know this, but in objective C I would just call [super getArgs:args] (if that were the language/syntax) in order to reference the parent. Is there a way to do this in C++?
Thanks,
Sean
Last edited on
You will have to pass an instance of the class to the function. You can't really do what you are asking "easily" because what if I did this:

1
2
CMethod0173::Args some_random_obj;
some_random_obj.getArgs(some_args); //what would this call? 
Right, maybe in objective C it's "super" or something that references the base class generically. If this weren't nested, I could just call the this->CMethod0173.getArgs(some_args); to distinguish from this->CMethod172.getArgs(some_args);
Thus, somehow I need to go back into the nest or reference the parent or super class generically, if there is a way to do it. I also tried:
1
2
3
4
5
void CMethod0173::Args::getArgs(AlignArgs *args)
{
    using CMethod172::Args::getArgs(AlignArgs *args);
    getArgs(args);
}

but it said that a class qualified name, CMethod172::Args::getArgs(AlignArgs*args); was not allowed, starting at the CMethod172...
Just to clarify, I tried some guesses:
1
2
3
4
void CMethod0173::Args::getArgs(AlignArgs *args)
{
    super::getArgs(args);
}

This doesn't work because "super" is not recognized.
nor:
1
2
3
4
void CMethod0173::Args::getArgs(AlignArgs *args)
{
    parent::getArgs(args);
}

This doesn't work because "parent" is not recognized.
nor:
1
2
3
4
void CMethod0173::Args::getArgs(AlignArgs *args)
{
    base::getArgs(args);
}

This doesn't work because "base" is not recognized.
Last edited on
Got it! Type casting...
1
2
3
4
void CMethod0173::Args::getArgs(AlignArgs *args)
{
    ((CMethod172::Args*)this)->getArgs(args);
}

and I should also mention that the nested class has to inherit separately:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef CMETHOD0173_H
#define	CMETHOD0173_H
#include"CMethod0172.h"
class CMethod172;
class CMethod0173 : public CMethod172
{
public:
    CMethod0173(AlignArgs *args);
    class Args: public CMethod172::Args
    {
    public:
        void getArgs(AlignArgs *args);
    };
private:
};
#endif	/* CMETHOD0173_H */  

Last edited on
Topic archived. No new replies allowed.