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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:09:01+00:00 2026-05-25T16:09:01+00:00

In building a class structure, I would like to have derived classes potentially posses

  • 0

In building a class structure, I would like to have derived classes potentially posses derived member classes. For example:

class GamePiece
{ 
}
class Checker : GamePiece
{}
class ChessMan : GamePiece
{}

class Game 
{
   protected GamePiece _piece;
}

class Checkers : Game
{
   Checkers(){ _piece = new Checker(); }
}
class Chess : Game
{
   Chess(){_piece = new ChessMan();}
}

This is seriously oversimplified, but makes the actual point. Now assuming that there are events and such, I would like to attach the common ones in the base class constructor, and the specialized ones in the derived constructor. For example both a checker and a chessman might have a “captured” event and a “moved” event. I would like to attach them in the base constructor. However, events that would be specific such as “castled” or something like that would be attached only in the specific constructor.

The problem I have is that the base constructor seems to only be able to run BEFORE the derived constructor. How do I effect this such that I get to actually instantiate the “gamepiece” before I call the base “Game” constructor to attach to events.

My gut suggests that this is better handled by removing that functionality from the constructor and simply having an “Attach()” member function and handling it there. However, I want to make sure I am going in the right direction, since it would seem there should be a way to do it in the constructor.

  • 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-25T16:09:02+00:00Added an answer on May 25, 2026 at 4:09 pm

    You could try the Template design pattern:

    class Game
    {
        Game() { Init(); }
        protected virtual void Init() {}
    }
    

    Now you can insert generic event handling in the Game Init method and override it with concrete handling logic in the descendants.

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

Sidebar

Related Questions

I have a data structure that represents C# code like this: class Namespace: string
I have a object structure like this: public class Entity { IList<Relationship> Relationships{get;set;} }
imagine this layout of classes i use for building a tree structure: class Treenodebase
I'm building a class library that will have some public & private methods. I
I am building a class that handles NSURLConnection requests. To allow other classes to
I am building a Windows form application using .NET 3.5. I would like to
I am building an XNA game with the following simplified structure: class Entity {
I have a project using autoconf and automake with following structure: / src/ class.h
I'm building a datastructure class with an std-like interface, and implementing different iterators for
I have a weird problem. I have a vector that I would like to

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.