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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:50:42+00:00 2026-05-26T17:50:42+00:00

I am having trouble with forcing data type changes has on my own objects.

  • 0

I am having trouble with forcing data type changes has on my own objects. I have a base class say A and two classes derived from A called B and C. I pass objects B and C to a function that checks which type of object it is (B or C). Here is some example code below and the question to my problem:

enum ClassType {"B", "C"};

class A {
  protected:
     m_Type;

  public:
     ClassType Type() { return m_Type}
     ...
     ...
};

class B : public A {
   otherMemberFunctions();

}

 class C : public A {
   otherMemberFunctions();

}


void WhatType(vector<A*>* candidates){


  vector<B*> b_candidates(0);
  vector<C*> c_candidates(0);

  for(int i = 0; i < candidates->size(); i++){

    if(candidates->at(i)->Type() == B ){

      B* b = (B*) candidates->at(i);
      b_candidates(b);
   }

  //Same idea for Object C
  }
}

I would then use WhatType(vector<A*>* candidates) as follows

vector<B*>* b_example

WhatType((vector<A*>*) b_exmaple)

When I have filled the new vector b_candidates in the function WhatType. Will I still have access to the member functions in the B object or will I only have the access to the member functions in the base class A?

I am confused to what happens with the object when I change the type of the object.

Here

WhatType((vector<A*>*) b_exmaple)

and here

B* b = (B*) candidates->at(i);
  • 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-26T17:50:43+00:00Added an answer on May 26, 2026 at 5:50 pm

    When you receive a pointer to a polymorphic object you have two types: the “static” type of the object, which, in your case, will be A *, and its “dynamic” or “real” type, that depends on what was actually assigned to it.

    Casting your A * to B * forces the compiler to consider that pointer as a pointer to B; this is safe as long as you actually know that that pointer is actually a pointer to B, otherwise the compiler will start writing nonsensical code (invoking B methods on data of another type).

    The checks you are trying to implement are a homegrown version of RTTI, which is a mechanism that allows you to know which is the “real type” of a pointer or a reference to a polymorphic class, and to perform that kind of casts safely. Check out typeid and dynamic_cast on your C++ manual for more info about it. (Incidentally, IIRC dynamic_cast is not only for safety in case the dynamic type is wrong, but it may perform also some extra magic on your pointer if you use it in complicated class hierarchies; so, avoid C-style casting for polymorphic classes)

    By the way, in general it’s considered “code smell” to have to manually check the “real type” of the pointer in order to cast it and use its methods: the OOP ideal would be being able to do the work only though virtual methods available in the base class.


    Big warning: RTTI works only on polymorphic classes, i.e. classes that have at least one virtual method. On the other hand, if you are building a class hierarchy where objects are being passed around as pointers to the base class you’ll almost surely want to have a virtual destructor, so that’s no big deal.

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

Sidebar

Related Questions

Having trouble inheriting from a template class. Looks something like this: template<typename type> class
Having trouble when trying to create a class using another class(and 2 inner classes),
Having Trouble with Entity Framework. I have been populating EntityReferences with an EntityKey inorder
I having trouble in dividing the HTML frames. I have been using the following
I am having trouble with IE7. I have a header, which is an IMG.
I am having trouble doing something that is probably pretty simple! I have a
Having trouble with some select statements. I have 2 tables. sms_log and sms_messages. sms_log
I have a model which uses acts-as-tree. For example: class CartoonCharacter < ActiveRecord::Base acts_as_tree
Having trouble with grepping and cutting at the same time I have a file
I'm having trouble saving changes made to my POCO's back into the database. When

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.