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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:59:30+00:00 2026-05-27T01:59:30+00:00

How do I determine the type of a class that is related to another

  • 0

How do I determine the type of a class that is related to another class at runtime?

I have figured out a solution, the only problem is that I ended up having to use a define that has to be used in all of the derived classes.

Is there a simpler way to do this that doesn’t need the define or a copy paste?

Things to note: both the class and the related class will always have their respective base class, the different classes can share a related class, and as in the example I would like the control class to own the view.

#include <iostream>
#include <string>

class model;

class view {
public:
  view( model *m ) {}
  virtual std::string display() {
    return "view";
  }
};

#define RELATED_CLASS(RELATED)\
typedef RELATED relatedType;\
virtual relatedType*createRelated(){\
return new relatedType(this);}

class model {
public:
  RELATED_CLASS(view)
  model() {}
};

class otherView : public view {
public:
  otherView( model *m ) : view(m) {}
  std::string display() {
    return "otherView";
  }
};

class otherModel : public model {
public:
  RELATED_CLASS(otherView)
  otherModel() {}
};

class control {
public:
  control( model *m ) : m_(m),
      v_( m->createRelated() ) {}
  ~control() { delete v_; }
  std::string display() {
    return v_->display();
  }
  model *m_;
  view  *v_;
};

int main( void ) {
  model m;
  otherModel om;

  model *pm = &om;

  control c1( &m );
  control c2( &om );
  control c3( pm );

  std::cout << c1.display() << std::endl;
  std::cout << c2.display() << std::endl;
  std::cout << c3.display() << std::endl;
}
  • 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-27T01:59:30+00:00Added an answer on May 27, 2026 at 1:59 am

    You could use templates instead to avoid #defines, but, either way, note that you’re essentially overloading on return type with otherModel’s createRelated method.

    There’s probably a better way to do what you’re trying to do.

    #include <iostream>
    #include <string>
    
    class model;
    
    class view {
    public:
      view( model *m ) {}
      virtual std::string display() {
        return "view";
      }
    };
    
    class model {
    public:
      virtual view* createRelated() {  return new view (this); }
      model() {}
    };
    
    
    template <class Related>
    class relatedModel : public model
    {
      public:
        relatedModel() : model() {}
        virtual view* createRelated() { return new Related(this);}
    } ;
    
    
    class otherView : public view {
    public:
      otherView( model *m ) : view(m) {}
      std::string display() {
        return "otherView";
      }
    };
    
    class control {
    public:
      control( model *m ) : m_(m),
          v_( m->createRelated() ) {}
      ~control() { delete v_; }
      std::string display() {
        return v_->display();
      }
      model *m_;
      view  *v_;
    };
    
    int main( void ) {
      relatedModel<view> m;
      relatedModel<otherView> om;
    
      model *pm = &om;
    
      control c1( &m );
      control c2( &om );
      control c3( pm );
    
      std::cout << c1.display() << std::endl;
      std::cout << c2.display() << std::endl;
      std::cout << c3.display() << std::endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I determine the type of a class that is related to another
I have a Class object. I want to determine if the type that the
I have an event handler that needs to determine a type and execute code
I have a method that receives something and it needs to determine the type
I have that template class that uses a policy for it's output and another
I need to use a class that I have written as the type of
I have an interesting problem. I'd like to create a generic class that can
I have a class that contains a variable of indeterminate type, which must be
Given a FILE*, is it possible to determine the underlying type? That is, is
I have a class that implements an interface like this; ... = new Location<ICafe>();

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.