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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:58:36+00:00 2026-06-12T06:58:36+00:00

In my project I have a tree of QObjects with different types. Let me

  • 0

In my project I have a tree of QObjects with different types. Let me give you a simple example which should give you the idea of what I’m talking about. This could be an exemplary QObject tree (not an inheritance diagram but somewhat similar to a class diagram), where we start with the root object at the top and listing its child objects below:

                                   City
                                     |
                                  Street
                                 /      \
                            House       ...
                           /     \
                       Floor     ...
                       /   \
                   Room    ...

While a QObject tree does not necessarily follow this rule, in my case very class in the tree has a parent of one particular type. So, to stay at one relation as an example, a house can have some floors and children of other types, but a floor is a child of a house and only a house.

Now if I model these as QObject derived classes, the interface of class Floor should give me its House *house() by internally looking what the QObject::parent() is. I really know that this parent will be of type House*, because I designed it to be so and the programmer(s) stick to this design.

Is it ok if I C-style cast the QObject *parent() to House* to implement House *house() const?

Qt suggests to use qobject_cast<House*>(parent()), returning 0 if the QObject* parent() doesn’t inherit from House, making the cast type-safe. But in release mode, I want to avoid such slow casts. I profiled one particular algorithm which is performing three times faster when doing C-style casts instead of qobject_casts. This is because qobject_cast asks the meta object about the type-info during runtime, resulting in considerable slowdowns when called very often.

So what I ended up with was:

House *Floor::house() const {
    Q_ASSERT(qobject_cast<House*>(parent()));
    return (House*)parent();
}

This will assert in debug mode that the parent really is a House, while just C-style-casting efficiently in release mode.

To come to the point: I know people will yell when C++ programmers do C-style casts, but will it be ok in this situation?

An alternative solution would be to store the parent pointers of the particular types additionally as member variables, but I think this is redundant and I also like that the tree is reflected in those methods returning the tree parents.

  • 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-06-12T06:58:37+00:00Added an answer on June 12, 2026 at 6:58 am

    If you’re sure that the ownership graph won’t change, even during maintenance, then a cast that isn’t type-checked is safe. You can use static_cast<House*>(parent()), which is fast, or, if you really want to, a C-style cast is okay. But the C-style cast is not faster than the C++ static_cast.

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

Sidebar

Related Questions

I have a hobby project which is about creating a tree to store identification
I have a business object project, which contains composite structure: public class Tree {
For a new project I have created a local folder tree with empty Visual
I have the following project-tree: I can access the 290.gif file in the path:
I have project A, a C# console application which I want to reference my
I have a project where I would like to create a dynamical directory tree
I have a project that sits at the top of a complicated tree of
For one of my projects I have a tree of QObject derived objects, which
I have a Project on CodePlex which is using TFS and I am using
I have a large tree-like data structure of objects which behave mostly identical but

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.