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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:11:09+00:00 2026-05-30T09:11:09+00:00

class Product { … } class Perishable : public : Product { public: int

  • 0
class Product
{
...
}

class Perishable : public : Product
{
 public:
 int getday();

}


int main()
{
   Product *temp;
   //due to some coding
   //temp could point to either Perishable object or Product object that is determine           //during runtime
   cout<< ((Perishable*)temp)->getday() ;// is there other way to achieve this typecasting seems dangerous

The problem with this code is that if temp point to a Product object, temp->getday() would be invalid and i do not know how to prevent this from happening. If due to some circumstances , i am only allowed to have getday() in Perishable but not in Product, how could i check whether temp is pointing to a perishable object or a Product object?

Some help would be appreciated/

}

  • 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-30T09:11:11+00:00Added an answer on May 30, 2026 at 9:11 am

    “The problem with this code is that if temp point to a Product object, temp->getday() would be invalid and i do not know how to prevent this from happening.”

    In the spirit of the question, and if you absolutely do not want to declare/implement getday() in your Product class as mentioned in the other answers, you can use dynamic cast to determine the runtime type of your variable, and then only call getday() if you have a Perishable instance:

      Product* pPerishable = new Perishable;
      Product* pProduct = new Product;
      Perishable * pActualPerishable;
    
      pActualPerishable= dynamic_cast<Perishable *>(pPerishable );
      //pActualPerishable will not be null because it is of type Perishable at run time
    
      pActualPerishable = dynamic_cast<Perishable*>(pProduct );
      //pActualPerishable will be null because you are trying to cast a runtime base type to a derived type.
    

    So, try to dynamic cast your variable to a Perishable, and if successful then you know you can call getday(). Note this is no longer polymorphic, but determining the type at run time has its uses especially if you don’t have control over the interface of the objects you are working on.

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

Sidebar

Related Questions

Given the following object: public class Product { string Name {get;} int Quantity {get;}
I have one webservice: public class Product { public int ProductId { get; set;
I have the following model: public class Product { public int Id { get;
I have two POCO objects: public class Product { public int ProductID { get;
Consider the following Entities. public class Product{ int id; Date effectiveDate; Date expiryDate; Set<Inventory>
I have these classes: public class Product { [Key] public virtual int ProductId {
I have a standard Domain Layer entity: public class Product { public int Id
Following is my Model for Product public class Product { public int Id {
I have following entities: public class Product { [Key] public int Id{get;set;} //other properties
I have two entities: public class Product { [HiddenInput(DisplayValue=false)] public int ProductID { get;

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.