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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:58:29+00:00 2026-05-27T13:58:29+00:00

It’s hard to explain exactly what I want to do here, but I have

  • 0

It’s hard to explain exactly what I want to do here, but I have a base class and two classes which inherit this base class. Both classes which inherit it have their own unique members. I want to be able to pass both to a method, and have that method detect which it is, then access their unique members. I can’t assume there will only be two classes which inherit it, so i’m looking for something of a more general solution.

Here is an example of what I’d like to do:

#include <iostream>

class Base {

  public:
    int _type;
    Base() { }
};

class First : public Base {
  public:
    int _first_only;

    First() { }
};

class Second : public Base {
  public:
    int _second_only;

    Second() { }
};

void test (Base b) {

  std::cout << "Type: " << b._type << std::endl;

  if(b._type==1) {
    std::cout << "First\n";
    // Want to be able to do this
    std::cout << "Val: " << (First)b._first_only << std::endl; 
  } else if(b._type==2) {
    std::cout << "Second\n";
    // And this
    std::cout << "Val: " << (Second)b._second_only << std::endl;
  }
}

int main() {

  First f;
  f._first_only=1;
  f._type=1;
  Second s;
  s._type=2;
  s._second_only=2;

  test(f);
  test(s);
}
  • 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-27T13:58:30+00:00Added an answer on May 27, 2026 at 1:58 pm

    This is similar to others answers:

    1. You can write polymorphic classes to get this behavior using virtual functions.
    2. Pass the Dervied class objects either by pointer or by reference to get polymorphic behaviour. Otherwise it will lead to object slicing. Your test() function leads to object slicing.

    This code may also help you. You can see that there are different ways to print the type. I used GetBaseType(), GetDerivedType() and GetType(). Among these GetType() method is convenient for you case. There are two constructors for convenience. Constructors allow to initialize data members.

    class Base {
    private:
        int _type;
    public:
        Base(int type) : _type(type) { }
        int GetBaseType() { return _type; }
        virtual int GetDerivedType() = 0;
        virtual int GetType() { return _type; }
    };
    
    class First : public Base {
    private:
        int _first_only;
    public:
        First() : Base(1), _first_only(1) { }
        First(int first_only) : Base(first_only), _first_only(first_only) { }
        int GetDerivedType() { return _first_only; }
        virtual int GetType() { return _first_only; }
    };
    
    class Second : public Base {
    private:
        int _second_only;
    public:
        Second() : Base(2), _second_only(2) { }
        Second(int second_only) : Base(second_only), _second_only(second_only) { }
        int GetDerivedType() { return _second_only; }
        virtual int GetType() { return _second_only; }
    };
    
    void test (Base &b) {
        std::cout << "Type: " << b.GetBaseType() << std::endl;
        std::cout << "Type: " << b.Base::GetType() << std::endl;
    
        std::cout << "Dervied type: \n";
        std::cout << "Val: " << b.GetDerivedType() << std::endl; 
        std::cout << "Val: " << b.GetType() << std::endl; 
    }
    
    int main() {
    
      First f(1);
      Second s(2);
    
      test(f);
      test(s);
    
      First f1;
      Second s1;
    
      test(f1);
      test(s1);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have an autohotkey script which looks up a word in a bilingual dictionary
I have an array which has BIG numbers and small numbers in it. I

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.