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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:37:49+00:00 2026-05-26T14:37:49+00:00

I am using C++ under Ubuntu 11.10 and the latest version of NetBeans. Let’s

  • 0

I am using C++ under Ubuntu 11.10 and the latest version of NetBeans. Let’s say I have the
following code:

class Node {}
class DerivedNode : public Node {}

class Graph {
    vector<Node*> nodes;
}

class DerivedGraph : public Graph { }

At the moment I’m storing DerivedNodes in the DerivedGraph class like this for example:

nodes.push_back(new DerivedNode());

When I need to use specific methods that only apply to DerivedNodes and DerivedGraphs
I am forced to use a dynamic_cast on my Node pointers first.

I would like to be able to have specific methods in DerivedGraph which apply only to DerivedNodes
and avoid the need of casting pointers. I do not mind having to redesign my classes if the end
result is better than what I have.

I am sure there must be a clean and simple method to achieve the same thing I’m trying to do.
Maybe something with specialized templates? Any thoughts on the matter would be greatly
appreciated. I’ll also provide any additional information required in the case I haven’t been too
clear.

EDIT: I don’t have two copies. I wanted to put emphasis on how it looks. I apologize for the presentation. What I want to obtain is:

class DerivedGraph: public Graph {
    vector<DerivedNode*> nodes;
}    
  • 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-26T14:37:49+00:00Added an answer on May 26, 2026 at 2:37 pm

    Are you sure that your interface in Node is appropriate? Sometimes when you find yourself needing to downcast (especially in a case like this where base pointers are stored in a container) that may be a signal that your abstract interface doesn’t cover all your needs properly. Often something like the Template Method pattern solves all your needs without needing a downcast at all.

    However, assuming that your inheritance model really need work in such a way, what you probably want to do is have virtual methods that get overridden in DerivedGraph for adding and getting nodes. You will have to verify the node type and downcast it in this case.

    One final approach is to have two separate containers, one in the parent that contains all nodes that aren’t DerivedNode and then another container in DerivedGraph that contains all the DerivedNode. Then you use overridden functions again to determine which container to access depending on your API needs.

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

Sidebar

Related Questions

I am developing a project in C++ under Ubuntu 11.10 using the latest version
I am learning rails by following the Rails Tutorial under Ubuntu. I have been
I'm using Git under Ubuntu Linux to sync and deploy my projects. I have
I am working in c++ under ubuntu. I have the following example: [car.h] #ifndef
I'm having a strange problem with Zend Framework under Ubuntu 11.10. I'm using a
I have a WPF application using Aero Glass. When using the application under a
On Linux under X11 and using GTK+ you have something called Main Loop. Once
I'm using nasm under ubuntu. By the way i need to get single input
Just curious. Using gcc/gdb under Ubuntu 9.10. Reading a C book that also often
I'm compiling Ruby 1.9.1-p376 under Ubuntu 8.04 server LTS (64-bit), by doing the following:

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.