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 4003912
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:12:34+00:00 2026-05-20T08:12:34+00:00

For context, I’m working on a C++ artificial-life system involving agents controlled by recurrent

  • 0

For context, I’m working on a C++ artificial-life system involving agents controlled by recurrent neural networks, but the details aren’t important.

I’m facing a need to keep two object hierarchies for the “brain” and “body” of my agents separate. I want a variety of different brain and body types that can be coupled to each other at run-time. I need to do this to avoid a combinatorial explosion caused by the multiplicative enumeration of the separate concerns of how a body works and how a brain works.

For example, there are many topologies and styles of recurrent neural network with a variety of different transfer functions and input/output conventions. These details don’t depend on how the body of the agent works, however, as long as sensory inputs can be encoded into neural activity and then decoded into actions.

Here is a simple class hierarchy that illustrates the problem and one potential solution:

// Classes we are going to declare

class Image2D; // fake
class Angle2D; // fake

class Brain;    
class Body;      
class BodyWithEyes;
class BrainWithVisualCortex;

// Brain and Body base classes know about their parallels

class Brain
{
  public:
    Body* base_body;

    Body* body() { return base_body; }
    virtual Brain* copy() { return 0; } // fake

    // ...etc
};

class Body
{
  public:
    Brain* base_brain;

    Brain* brain() { return base_brain; }
    virtual Body* reproduce() { return 0; } // fake

    // ...etc
};

// Now introduce two strongly coupled derived classes, with overloaded access
// methods to each-other that return the parallel derived type

class BrainWithVisualCortex : public Brain
{
  public:
    BodyWithEyes* body();
    virtual void look_for_snakes();
    virtual Angle2D* where_to_look_next() { return 0; } // fake
};

class BodyWithEyes : public Body
{
  public:
    BrainWithVisualCortex* brain();
    virtual void swivel_eyeballs();
    virtual Image2D* get_image() { return 0; } // fake
};

// Member functions of these derived classes

void BrainWithVisualCortex::look_for_snakes()
{
  Image2D* image = body()->get_image();

  // ... find snakes and respond
}

void BodyWithEyes::swivel_eyeballs()
{
  Angle2D* next = brain()->where_to_look_next();

  // ... move muscles to achieve the brain's desired gaze
}

// Sugar to allow derived parallel classes to refer to each-other

BodyWithEyes* BrainWithVisualCortex::body()  
{ return dynamic_cast<BodyWithEyes*>(base_body); }

BrainWithVisualCortex* BodyWithEyes::brain() 
{ return dynamic_cast<BrainWithVisualCortex*>(base_brain); }

// pretty vacuous test
int main()
{
  BodyWithEyes* body = new BodyWithEyes;
  BrainWithVisualCortex* brain = new BrainWithVisualCortex;
  body->base_brain = brain;
  brain->base_body = body;
  brain->look_for_snakes();
  body->swivel_eyeballs();
}

The trouble with this approach is that it’s clunky and not particularly type-safe. It does have the benefit that the body() and brain() member functions provide a bit of sugar for derived classes to refer to their partners.

Does anyone know of a better way of accomplishing this tight coupling between ‘parallel’ hierarchies of classes? Does this pattern come up often enough to have warranted a well-known general solution? A perusal of the usual sources didn’t reveal any established patterns that match this problem.

Any help appreciated!

  • 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-05-20T08:12:34+00:00Added an answer on May 20, 2026 at 8:12 am

    I think what you are doing is approximately correct. You would want the members such as reproduce to be pure virtual, though, so the base classes cannot be created. What is your issue with type-safety? You don’t want the Brain subclass and the Body subclass to depend on each others’ types.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Some context: one of the systems I'm working on is a .net 2.0 web
NHibernate.Context.WebSessionContext , otherwise known as web session context , plays nicely with ASP.NET, but
Context: I'm in charge of running a service written in .NET. Proprietary application. It
Context: So, I am attempting to build a ridiculously complex domain model. Talking with
Context: I have a WPF App that uses certain unmanaged DLLs in the D:\WordAutomation\MyApp_Source\Executables\MyApp
Context : programming a c/c++ win32-mfc library How to know whether we are in
Context PHP 5.3.x Overview After doing a code-review with an associate who uses both
For context, I am something of an emacs newbie. I haven't used it for
Some context upfront: Imagine a 200+ developers company finally setting up a more or
in context of SQL Server 2005, I have a table for which the primary

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.