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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:41:14+00:00 2026-05-26T20:41:14+00:00

Setup I have a graph library where I am trying to decompose things as

  • 0

Setup

I have a graph library where I am trying to decompose things as much as possible, and the cleanest way to describe it that I found is the following: there is a vanilla type node implementing only a list of edges:

class node
{
   public:
      int* edges;
      int edge_count;
};

Then, I would like to be able to add interfaces to this whole mix, like so:

template <class T>
class node_weight
{
   public:
      T weight;
};

template <class T>
class node_position
{
   public:
      T x;
      T y;
};

and so on. Then, the actual graph class comes in, which is templated on the actual type of node:

template <class node_T>
class graph
{
   protected:
      node_T* nodes;

   public:
      static graph cartesian(int n, int m)
      {
         graph r; 

         r.nodes = new node_T[n * m];

         return r;
      }
};

The twist is that it has named constructors which construct some special graphs, like a Cartesian lattice. In this case, I would like to be able to add some extra information into the graph, depending on what interfaces are implemented by node_T.

What would be the best way to accomplish this?

Possible solution

I thought of the following humble solution, through dynamic_cast<>:

template <class node_T, class weight_T, class position_T>
class graph
{
   protected:
      node_T* nodes;

   public:
      static graph cartesian(int n, int m)
      {
         graph r;

         r.nodes = new node_T[n * m];

         if (dynamic_cast<node_weight<weight_T>>(r.nodes[0]) != nullptr)
         {
            // do stuff knowing you can add weights
         }

         if (dynamic_cast<node_position<positionT>>(r.nodes[0]) != nullptr)
         {
            // do stuff knowing you can set position
         }

         return r;
      }
};

which would operate on node_T being the following:

template <class weight_T, class position_T>
class node_weight_position : 
      public node, public node_weight<weight_T>, public node_position<position_T>
{
    // ...
};

Questions

Is this — philosophically — the right way to go? I know people don’t look nicely at multiple inheritance, though with “interfaces” like these it should all be fine.

There are unfortunately problems with this. From what I know at least, dynamic_cast<> involves quite a bit of run-time overhead. Hence, I run into a problem with what I had solved earlier: writing graph algorithms that require weights independently of whether the actual node_T class has weights or not. The solution with this ‘interface’ approach would be to write a function:

template <class node_T, class weight_T>
inline weight_T get_weight(node_T const & n)
{
   if (dynamic_cast<node_weight<weight_T>>(n) != nullptr)
   {
      return dynamic_cast<node_weight<weight_T>>(n).weight;
   }

   return T(1);
}

but the issue with it is that it works using run-time information (dynamic_cast), yet in principle I would like to decide it at compile-time and thus make the code more efficient.

If there is a different solution that would solve both problems, especially a cleaner and better one than what I have, I would love to hear about it!

  • 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-26T20:41:15+00:00Added an answer on May 26, 2026 at 8:41 pm

    What about type traits? If you have a compiler handy that supports parts of C++11 already, there is std::is_base_of in the <type_traits> header.

    If you do not, there is boost with the same type trait.

    Now, to really be able to use it, you need some meta programming:

    // in the class...
    //  branch on whether the node type has weights
    static void set_weights(node_T* nodes, std::true_type){
        // has weights, set them
        // ...
    }
    
    static void set_weight(node_T* nodes, std::false_type){
        // doesn't have weights, do nothing
    }
    
    // in the function...
    typedef std::is_base_of<node_weight<weight_T>, node_T>::type has_weights;
    set_weight(nodes, has_weights());
    

    This works thanks to some magic that lets the nested typedef type be either true_type or false_type based on whether the type trait is true or false. We need the meta programming (branching through overloads), because accessing members that aren’t there will result in a compiler error, even if the access was in a branch that would never be executed.

    I hope that makes any sense at all, it’s quite difficult to type an answer to this topic on an iPod Touch…

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

Sidebar

Related Questions

I have currently the following setup: An object graph of all requests read from
I have a object graph that is setup like this Clients string Name List[Address]
Setup I have a website that draws RSS feeds and displays them on the
Setup: I have a COM DLL that calls a method inside a managed C#
I have following setup A -> B -> C A-Mapping: <hibernate-mapping> <class name=db.base.A table=A>
I have the following setup. Spring 3.0.5 Hibernate 3.5.6 MySql 5.1 To save a
I'm trying to get my facebook graph oauth setup using javascript's window location methods.
I have several functions on graph f(), g() and h() that implements different algorithms
I have a view controller in my iphone app setup so that when I
I am trying to setup facebook subscriptions for checkins, I have already asked for

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.