Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Private Nested Abstract Class

So maybe this is a dumb question and I'm over thinking this, but I have the following situation. I am making a "class Shell" which can run abstract "class Action" objects. It is the only class that should create or use these objects. Action objects need access to the Shell to perform specific actions on it, but I'm trying to avoid adding public interfaces for this (no one else should be allowed to do that).

I originally had a simple (not so elegant)

class Shell
{
 public:
    bool checkThing();
    // etc...
 private:
    bool _thing;
};

class Action
{
 public:
    virtual void execute( Shell &s )=0;
};

class ChangeAction : public Action
{
 public:
    void execute( Shell &s )
    {
        // requires friendship or public mutator!
        s._thing = true;
    }
};

So I considered a nested class Action, but I wanted to make it private (why let anyone else make concrete Actions except the Shell, right?)

class Shell
{
 public:
    bool checkThing();
    // etc...
 private:
    bool _thing;
    class Action;
};

class Shell::Action
{
 public:
    virtual void execute( Shell &s )=0;
};

class ChangeAction : public Shell::Action
{
 public:
    void execute( Shell &s )
    {
        // ok now!
        s._thing = true;
    }
};

But of course I can't inherit from Action any more (that makes sense, it's private). So that doesn't work.

So my question, should I just go with the first method and friendship or a public interface? Can I use something similar to the second method to keep that relationship with Actions and the Shell? Do you have a better idea?

like image 218
Matt Avatar asked Feb 16 '11 07:02

Matt


People also ask

Can abstract class have nested class?

It does not matter whether your class is abstract or concrete, as long as the nested class is either public , protected or the subclass is in the same package and the inner class is package private (default access modifier), the subclass will have access to it.

Can abstract class private?

Abstract classes can have private methods. Interfaces can't. Abstract classes can have instance variables (these are inherited by child classes).

Can nested classes access private members?

A nested class is a member of its enclosing class. Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private.

Can you inherit a nested class?

A static nested class can inherit: an ordinary class. a static nested class that is declared in an outer class or its ancestors.


1 Answers

If the only code that needs to be able to see Action is Shell, one option would be to forward-declare Action in the header file but only define the class in the .cpp file. This would then let you declare as many Action subclasses as you'd like in the implementation file without letting anyone else subclass off of Action because no one else would have a complete class definition for Action. This also avoids any need for public interfaces or friend declarations - all the Action classes are declared in the global scope, but shielded from other files by virtue of being declared in the .cpp file.

Great question, by the way!

like image 85
templatetypedef Avatar answered Sep 30 '22 07:09

templatetypedef