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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:52:21+00:00 2026-05-23T21:52:21+00:00

I have the following classes: class State { protected: Vec3D accel; Vec3D gyro; Vec3D

  • 0

I have the following classes:

class State
{
  protected:
    Vec3D accel;
    Vec3D gyro;
    Vec3D gps;
    float reward;
  public:
    boost::ptr_vector<Action> actions;
    ...
    virtual bool isTerm();
}

class guState : public State
{  
    float gps_stand_thres;
    float gps_down_thres;
  public:
    guState(Agent &A,ACTION_MODE &m);
    bool isTerm();
};

There are other classes which all inherit from State. Their differences solely lie on how they evaluate isTerm() which depends on behavior.
I would rather not use virtual functions bur override function isTerm, if it wasn’t for the fact that there are a few other templated classes which are designed to work with all sorts of State-derived classes.
One of them is Policy:

template <class S>
class Policy
{ 
  protected:    
    float QValue;
    S *state;
    S *nextState;
  public:
    ...
    template <class P>
    void updateOptimal(boost::ptr_vector<P> &policies);
}

updateOptimal has to obtain A State-derived class (depending on behavior), up-cast from a State *ptr to whatever the S-type being currently used is, in order to search for policies for that state. Now, because State-derived classes are polymorphic, I assumed it was the right thing to do:

S *s = dynamic_cast<S *>(iter->getNextState());

where the iter is an iterator of Actions and each action has a pointer of State *nextstate;
action->nextstate is set at some other point:

action->setNextState(dynamic_cast<State *>(state)); 

I could template the entire class Action, in order to avoid using State *nextstate; and use S *nextstate; but that would require an enormous amount of changes throughout the project.

Reading the casting tutorial on cplusplus.com it is my understanding that it is best to use dynamic_cast because it does a type check before up or down casting.
However in the following code after casting I do not do anything to the up casted state other than use it for searching:

P *temp_pol = var::findPolicy(policies,s);

where findPolicy is:

template <class P, class S>
P* findPolicy(boost::ptr_vector<P> &policies,S *state);
  • Would it be ok to skip safety checks, and use a static cast ? I
    have tried it, and it compiles.
  • Would it be ok to skip checks altogether, and do a reinterpret_cast ?
    I have also tried it and it compiles.
  • What is the penalty for doing a dynamic_cast ? I know theres a small
    overhead, but is it anything serious ?
  • Is there a way to upcast from State *ptr to S-type *ptr without using
    a polymorphic class (avoiding the virtual function and simply
    override it)?
  • 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-23T21:52:22+00:00Added an answer on May 23, 2026 at 9:52 pm

    The difference between static_cast and dynamic_cast is that an invalid static_cast is undefined behavior, while an invalid dynamic_cast results in a null-pointer or a bad_cast-exception (if you cast references). The penalty for a dynamic_cast is a type-check during runtime, and increased code-size due to RTTI.

    So, if you are sure that the cast is always fine, you can safely use static_cast. Using reinterpret_cast would not give you any performance-improvement over static_cast, so you shouldn’t use it here.

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

Sidebar

Related Questions

I have the following classes: class Vertex { public: float X; float Y; float
I have the following classes: public class Person { public String FirstName { set;
I have the following existing classes: class Gaussian { public: virtual Vector get_mean() =
I have the following classes: Public Class Email Private Shared ReadOnly EMAIL_REGEX = \b[a-zA-Z]+[a-zA-Z0-9._+-]+@
I have following classes. class A { public: void fun(); } class B: public
I have the following two classes: public class Address { public string AddressLine1 {
I have the following classes [XmlRoot] public class AList { public List<B> ListOfBs {get;
Lets say i have the following c# classes: abstract class a { protected abstract
I have the following two classes: package { import flash.display.Sprite; import flash.events.Event; public class
I have the following classes: class A { public: virtual void f() {} };

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.