CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2010
    Posts
    17

    Inheritance with static constraint

    HI.

    Is there any way I can use inheritance in order to enforce my derived class use a static member?
    Like:

    Code:
    class Base {
      ...
    }
    
    
    class Derived : public Base {
      public:
        static int _classNum;
    }
    So that every derived class would be enforced to use a static member?
    I thought about pure virtual methods (getter/setter) in the Base class,
    but they won't assure that the derived classes are defining the variable as static.

    Is there a logic solution for that (I got it as an assignment, with this constraint)

  2. #2
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Inheritance with static constraint

    Quote Originally Posted by gmeir11 View Post
    HI.

    Is there any way I can use inheritance in order to enforce my derived class use a static member?
    Like:

    Code:
    class Base {
      ...
    }
    
    
    class Derived : public Base {
      public:
        static int _classNum;
    }
    So that every derived class would be enforced to use a static member?
    I thought about pure virtual methods (getter/setter) in the Base class,
    but they won't assure that the derived classes are defining the variable as static.

    Is there a logic solution for that (I got it as an assignment, with this constraint)
    What exactly are you trying to do?

    Why must you enforce this staticness in your derived classes? Would defining the member inside a virtual method meet your needs?

    Code:
    class Base {
      virtual const std::string& get_name() = 0;
    }
    
    class Derived : public Base {
      public:
         virtual const std::string& get_name();
    }
    
    const std::string& Base::get_name()
    {
        static const std::string name = "Base";
        return name;
    }
    
    const std::string& Derived::get_name()
    {
        static const std::string name = "Base";
        return name;
    }
    Last edited by monarch_dodra; September 17th, 2010 at 02:20 AM.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  3. #3
    Join Date
    May 2009
    Posts
    2,413

    Re: Inheritance with static constraint

    Quote Originally Posted by gmeir11 View Post
    I thought about pure virtual methods (getter/setter) in the Base class, but they won't assure that the derived classes are defining the variable as static.
    This would be to enforce an implementation detail. Just define the pure virtual getter/setter methods in Base and specify their behaviour. Then it's up to any Derived class to make that come true. You can even hint that using a static variable probably is a good idea.

  4. #4
    Join Date
    Mar 2002
    Location
    Kent, United Kingdom
    Posts
    399

    Re: Inheritance with static constraint

    Would something like this be what you're looking for ?

    Code:
    template< typename T >
    class base {
    protected:
      static T the_data ;
    } ;
    
    class derived : public base< int > {
      // access the_data
    } ;
    your humble savant

  5. #5
    Join Date
    Sep 2010
    Posts
    17

    Re: Inheritance with static constraint

    As I understand it the class have that static variable,
    so that all derived classes have access to that variable.

    I need a way to enforce all derived classes to have a static variable each,
    different variables, each for every derived class.

    I believe there isn't, but can you think of a way to do that ?

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Inheritance with static constraint

    You could use the Curiously Recurring Template pattern (note the slight modification of the above):

    Code:
    class base
    {
        ...
    };
    
    template< typename T, typename Derived>
    class base_CRTP: public base
    {
    protected:
        static T the_data ;
    } ;
    
    class derived : public base_CRTP<int, derived>
    {
        // access the_data
    } ;
    Here the T template parameter is incidental; the magic is in requiring each derived class to inherit from base_CRTP<derived>. Each instantiation of this will be a different class, so therefor each derived type will have its own static member.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured