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

  • Home
  • SEARCH
  • 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 1083489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:26:33+00:00 2026-05-16T22:26:33+00:00

I really didn’t know how to specify the problem in the title, so here’s

  • 0

I really didn’t know how to specify the problem in the title, so here’s the gist of it.

I am writing graph classes Graph, Node, and Edge, and then subclassing them into VisGraph, VisNode and VisEdge to obtain a drawable graph (in C++). I then need to further subclass those into specific classes that depend on certain data. So I have a lot of parallel inheritance:

Graph -- VisGraph -- RouteGraph

Node  -- VisNode  -- RouteNode

Edge  -- VisEdge  -- RouteEdge

This is pretty ugly and I started out doing this, so that I would implement functionality incrementally, but there are a lot of problems. One of them, for example, is that the base class has a container of all the Node instances in the graph. The problem is, if I am in a function in VisGraph dealing with a VisNode, that requires functionality unique to VisNode, I have to do a dynamic_cast on the Nodes that I get from the container in the base class.

Perhaps I should write a “Vis” class that holds a Graph and draws it?
I found inheritance convenient because each node/edge could easily draw itself instead of me
storing extra information outside about position etc. and drawing them all individually.

Do you have any suggestions/design patterns that could make this more elegant?

Thank you in advance.

  • 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-16T22:26:33+00:00Added an answer on May 16, 2026 at 10:26 pm

    If in doubt, throw templates at the problem until it surrenders:

    template <typename N, typename E>
    class Graph {
        std::vector<N> nodes;
        std::vector<E> edges;
    };
    
    typedef Graph<VisNode, VisEdge> VisGraph;
    typedef Graph<RouteNode, RouteEdge> RouteGraph;
    

    You lose the inheritance (RouteGraph no longer inherits from VisGraph), but that’s normal in C++ for container types, and Graph is somewhat like a container. You can keep the inheritance between Node -> VisNode -> RouteNode, though.

    Since nodes and edges are supposed to be of matching types, you could go even further, and give Graph a single template parameter, which itself is a class containing the edge and node types as typedefs. I’m not sure it’s worth it, though.

    Edit

    Since you want to successively add functions, you could keep a form of inheritance but lose the polymorphism:

    template <typename N, typename E>
    class GraphImpl {
        std::vector<N> nodes;
        std::vector<E> edges;
    };
    
    template <typename N, typename E>
    class VisGraphImpl : public GraphImpl<N, E> {
        // constructors
    
        // extra functions
    };
    
    template <typename N, typename E>
    class RouteGraphImpl : public VisGraphImpl<N, E> {
        // constructors
    
        // extra functions
    };
    
    typedef GraphImpl<Node, Edge> Graph;
    typedef VisGraphImpl<VisNode, VisEdge> VisGraph;
    typedef RouteGraphImpl<RouteNode, RouteEdge> RouteGraph;
    

    There might be a better way, though, by bundling these extra functions up into sensible mixins and using CRTP:

    template<typename Derived>
    class VisFunctions {
        void somfunc() {
            myself = static_cast<Derived&>(*this);
            // do stuff
        }
    };
    

    Then:

    class VisGraph : public Graph<VisNode, VisEdge>, public VisFunctions<VisGraph> {
        friend class VisFunctions<VisGraph>;
    };
    
    class RouteGraph : public Graph<RouteNode, RouteEdge>, public VisFunctions<RouteGraph>, public RouteFunctions<RouteGraph> {
        friend class VisFunctions<RouteGraph>;
        friend class RouteFunctions<RouteGraph>;
    };
    

    Not sure how that’ll look for your real circumstances, though. Btw, if you don’t want/need the friend declaration for the extra functions, then you don’t need those extra functions to be members at all – just make them free functions that take a VisGraph or RouteGraph parameter.

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

Sidebar

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.