Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8080219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:20:51+00:00 2026-06-05T16:20:51+00:00

I am trying to make a class that will represent two different behaivours at

  • 0

I am trying to make a class that will represent two different behaivours at the same time. Like the following:

class IMouse {
public:
    virtual void Walk() const = 0;
};

class TSimpleMouse : public IMouse {
public:
    void Walk() const;
};

class IBat {
public:
    virtual void Fly() const = 0;
};

class TSimpleBat : public IBat {
public:
    void Fly() const;
};

template <class TMouse, class TBat>
class TSuperCreatureImpl {
public:
    void Do() const {
        Walk();
        Fly();
    }
};

typedef TSuperCreatureImpl<TSimpleMouse, TSimpleBat> TSimpleSuperCreature;

This is important for me to have the behaviour by deafult (method Do), because I would like to make different typedefs. It seems to be very simple.

But I also would like methods Fly and Walk to have parameters (velocity, for example). How should I modify the architecture to have an opportunity to use many typedefs for different creatures?

How to change the architecture if there are no default constructors for Mouse and Bat?

Thanks a lot.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T16:20:54+00:00Added an answer on June 5, 2026 at 4:20 pm
    template <class TMouse, class TBat>
    class TSuperCreature : public TMouse, public TBat
    {
      public:
        void do() const {
            this->walk();
            this->fly();
        }
    };
    

    But I also would like methods Fly and Walk to have parameters (velocity, for example). How should I modify the architecture to have an opportunity to use many typedefs for different creatures? Thanks a lot.

    You could give the functions default parameters…

    virtual void Walk(double meters_per_second = 1.1) const = 0;
    

    EDIT

    (from comment below) Imagine that we need 2 creatures: first one walks fast and flies slow, second one is the opposite. That means we have to write 2 typedefs. But I have no idea how to use velovity constants here. Which parts of architecture must have parameters?

    One option is to do something like this:

    template <class TMouse, class TBat, int walk_speed, int fly_speed>
    class TSuperCreature : public TMouse, public TBat
    {
      public:
        void do() const {
            this->walk(walk_speed);
            this->fly(fly_speed);
        }
    };
    

    If you want to use doubles, they’re not allowed as template arguments (at least not in C++03), but as a hack you could accept a pair of numbers and divide them inside the template, or more properly you could provide a policy class….

    template <class TMouse, class TBat, class Speeds>
    class TSuperCreature : public TMouse, public TBat
    {
      public:
        void do() const {
            this->walk(Speeds::walk_speed);
            this->fly(Speeds::fly_speed);
        }
    };
    
    struct Speeds__Fly_Fast__Walk_Slow   // yes I know double underscores are reserved - I don't care!
    {
        static const double walk_speed = 0.5; // m/s
        static const double fly_speed = 10;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to build a small class-like container that will make it a little
http://jsfiddle.net/JamesKyle/re73T/ Trying to make a class switcher that will do the following: On Load
I am trying to make a method for a class that will automatically insert
I'm trying to make a base class that uses actors, kind of like so:
I'm trying to make a class that will handle some router functionality such as
I'm trying to make a class that would draw objects of my JBox2d's world
I'm trying to make a class that can hold and later call functions. It
I'm trying to make a base class that defines the interfaces for all derived
Hi I am trying to make a global class that I can use in
I am trying to extend the mysqli class to make a helper class that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.