C2440 static_cast cannot convert from base class to derived class










0















I am attempting to understand why casting from a base class to an derived class using pointers compiles fine, but casting using non-pointer object produces the error C2440.



Below I have a base class ThreadedMessage that is inherited by class GPSMessage.



struct ThreadedMessage

ThreadedMessage()
: m_Type(0), m_ID(0)


ThreadedMessage(uint Type, uint ID = 0) :
m_Type(Type), m_ID(ID)


uint m_Type;
uint m_ID;
;

struct GPSMessage : public ThreadedMessage

GPSMessage()
: ThreadedMessage()


GPSMessage(double lat, double lon)
: ThreadedMessage(1), m_lat(lat), m_lon(lon)


double m_lat;
double m_lon;
;


In myFunction I am attempting to cast from the base class to the derived class.



void myFunction(const ThreadedMessage& msg)

const GPSMessage* message = static_cast<const GPSMessage*>(&msg); // Compiles fine
const GPSMessage message1 = static_cast<const GPSMessage>(msg); // Error C2440



The call to myFunction() looks like this:



GPSMessage msg(21.123, 12.321);
myFunction(msg);


When I compile, the casting of a pointer compiles fine, but the non-pointer casting fails with the following error:




error C2440: 'static_cast' : cannot convert from 'const ThreadedMessage' to 'const GPSMessage' No constructor could take the source type, or constructor overload resolution was ambiguous




Why am I unable to cast from the base class to the derived class with non-pointer variables?



Compiler is MS VS 2008 C++.



Yes, I have looked at other similar SO questions, but the ones I have read don't seem to answer my question.










share|improve this question






















  • On a side note: did you consider upgrading MS VC 2008 to something which is a bit more modern? Newer versions of MSVC improved a lot on error reporting and following the C++ standard. Not to mention support for c++11, c++14 and c++17

    – JVApen
    Nov 14 '18 at 7:44











  • MS VS 2008 was the last version of VS to support the system I am working on. Also for many systems C++11 and later is not allowed or supported.

    – Fred
    Dec 13 '18 at 19:55















0















I am attempting to understand why casting from a base class to an derived class using pointers compiles fine, but casting using non-pointer object produces the error C2440.



Below I have a base class ThreadedMessage that is inherited by class GPSMessage.



struct ThreadedMessage

ThreadedMessage()
: m_Type(0), m_ID(0)


ThreadedMessage(uint Type, uint ID = 0) :
m_Type(Type), m_ID(ID)


uint m_Type;
uint m_ID;
;

struct GPSMessage : public ThreadedMessage

GPSMessage()
: ThreadedMessage()


GPSMessage(double lat, double lon)
: ThreadedMessage(1), m_lat(lat), m_lon(lon)


double m_lat;
double m_lon;
;


In myFunction I am attempting to cast from the base class to the derived class.



void myFunction(const ThreadedMessage& msg)

const GPSMessage* message = static_cast<const GPSMessage*>(&msg); // Compiles fine
const GPSMessage message1 = static_cast<const GPSMessage>(msg); // Error C2440



The call to myFunction() looks like this:



GPSMessage msg(21.123, 12.321);
myFunction(msg);


When I compile, the casting of a pointer compiles fine, but the non-pointer casting fails with the following error:




error C2440: 'static_cast' : cannot convert from 'const ThreadedMessage' to 'const GPSMessage' No constructor could take the source type, or constructor overload resolution was ambiguous




Why am I unable to cast from the base class to the derived class with non-pointer variables?



Compiler is MS VS 2008 C++.



Yes, I have looked at other similar SO questions, but the ones I have read don't seem to answer my question.










share|improve this question






















  • On a side note: did you consider upgrading MS VC 2008 to something which is a bit more modern? Newer versions of MSVC improved a lot on error reporting and following the C++ standard. Not to mention support for c++11, c++14 and c++17

    – JVApen
    Nov 14 '18 at 7:44











  • MS VS 2008 was the last version of VS to support the system I am working on. Also for many systems C++11 and later is not allowed or supported.

    – Fred
    Dec 13 '18 at 19:55













0












0








0








I am attempting to understand why casting from a base class to an derived class using pointers compiles fine, but casting using non-pointer object produces the error C2440.



Below I have a base class ThreadedMessage that is inherited by class GPSMessage.



struct ThreadedMessage

ThreadedMessage()
: m_Type(0), m_ID(0)


ThreadedMessage(uint Type, uint ID = 0) :
m_Type(Type), m_ID(ID)


uint m_Type;
uint m_ID;
;

struct GPSMessage : public ThreadedMessage

GPSMessage()
: ThreadedMessage()


GPSMessage(double lat, double lon)
: ThreadedMessage(1), m_lat(lat), m_lon(lon)


double m_lat;
double m_lon;
;


In myFunction I am attempting to cast from the base class to the derived class.



void myFunction(const ThreadedMessage& msg)

const GPSMessage* message = static_cast<const GPSMessage*>(&msg); // Compiles fine
const GPSMessage message1 = static_cast<const GPSMessage>(msg); // Error C2440



The call to myFunction() looks like this:



GPSMessage msg(21.123, 12.321);
myFunction(msg);


When I compile, the casting of a pointer compiles fine, but the non-pointer casting fails with the following error:




error C2440: 'static_cast' : cannot convert from 'const ThreadedMessage' to 'const GPSMessage' No constructor could take the source type, or constructor overload resolution was ambiguous




Why am I unable to cast from the base class to the derived class with non-pointer variables?



Compiler is MS VS 2008 C++.



Yes, I have looked at other similar SO questions, but the ones I have read don't seem to answer my question.










share|improve this question














I am attempting to understand why casting from a base class to an derived class using pointers compiles fine, but casting using non-pointer object produces the error C2440.



Below I have a base class ThreadedMessage that is inherited by class GPSMessage.



struct ThreadedMessage

ThreadedMessage()
: m_Type(0), m_ID(0)


ThreadedMessage(uint Type, uint ID = 0) :
m_Type(Type), m_ID(ID)


uint m_Type;
uint m_ID;
;

struct GPSMessage : public ThreadedMessage

GPSMessage()
: ThreadedMessage()


GPSMessage(double lat, double lon)
: ThreadedMessage(1), m_lat(lat), m_lon(lon)


double m_lat;
double m_lon;
;


In myFunction I am attempting to cast from the base class to the derived class.



void myFunction(const ThreadedMessage& msg)

const GPSMessage* message = static_cast<const GPSMessage*>(&msg); // Compiles fine
const GPSMessage message1 = static_cast<const GPSMessage>(msg); // Error C2440



The call to myFunction() looks like this:



GPSMessage msg(21.123, 12.321);
myFunction(msg);


When I compile, the casting of a pointer compiles fine, but the non-pointer casting fails with the following error:




error C2440: 'static_cast' : cannot convert from 'const ThreadedMessage' to 'const GPSMessage' No constructor could take the source type, or constructor overload resolution was ambiguous




Why am I unable to cast from the base class to the derived class with non-pointer variables?



Compiler is MS VS 2008 C++.



Yes, I have looked at other similar SO questions, but the ones I have read don't seem to answer my question.







c++ casting polymorphism static-cast






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 6:56









FredFred

4281518




4281518












  • On a side note: did you consider upgrading MS VC 2008 to something which is a bit more modern? Newer versions of MSVC improved a lot on error reporting and following the C++ standard. Not to mention support for c++11, c++14 and c++17

    – JVApen
    Nov 14 '18 at 7:44











  • MS VS 2008 was the last version of VS to support the system I am working on. Also for many systems C++11 and later is not allowed or supported.

    – Fred
    Dec 13 '18 at 19:55

















  • On a side note: did you consider upgrading MS VC 2008 to something which is a bit more modern? Newer versions of MSVC improved a lot on error reporting and following the C++ standard. Not to mention support for c++11, c++14 and c++17

    – JVApen
    Nov 14 '18 at 7:44











  • MS VS 2008 was the last version of VS to support the system I am working on. Also for many systems C++11 and later is not allowed or supported.

    – Fred
    Dec 13 '18 at 19:55
















On a side note: did you consider upgrading MS VC 2008 to something which is a bit more modern? Newer versions of MSVC improved a lot on error reporting and following the C++ standard. Not to mention support for c++11, c++14 and c++17

– JVApen
Nov 14 '18 at 7:44





On a side note: did you consider upgrading MS VC 2008 to something which is a bit more modern? Newer versions of MSVC improved a lot on error reporting and following the C++ standard. Not to mention support for c++11, c++14 and c++17

– JVApen
Nov 14 '18 at 7:44













MS VS 2008 was the last version of VS to support the system I am working on. Also for many systems C++11 and later is not allowed or supported.

– Fred
Dec 13 '18 at 19:55





MS VS 2008 was the last version of VS to support the system I am working on. Also for many systems C++11 and later is not allowed or supported.

– Fred
Dec 13 '18 at 19:55












2 Answers
2






active

oldest

votes


















2














These two casts have differing meaning.



The first cast:



const GPSMessage* message = static_cast<const GPSMessage*>(&msg); // Compiles fine


This means that msg is actually a GPSMessage (or a derived) object. You ask the compiler to threat msg as a GPSMessage. No new object will be created, message will point to msg. If msg is not actually a GPSMessage (or derived), then this cast has Undefined Behavior.



Btw, the following cast have the same meaning (casting to a reference):



const GPSMessage& message = static_cast<const GPSMessage&>(msg); // same meaning, which results in a reference instead of a pointer


About the second cast:



const GPSMessage message1 = static_cast<const GPSMessage>(msg); // Error C2440


This means, that you create a new object, message1 from msg. You should give a way to make this conversion possible. For example, you should create a constructor for GPSMessage which has a ThreadedMessage parameter:



struct GPSMessage 
GPSMessage(const ThreadedMessage &); // needed constructor
;


Or create a conversion operator for ThreadedMessage to GPSMessage:



struct ThreadedMessage 
operator GPSMessage(); // conversion operator
;





share|improve this answer






























    2














    In the function the compiler only knows that msg is a reference to a ThreadedMessage, it doesn't know what's really passed as an argument inside the function.



    Think about what happened if you passed a reference to an actual ThreadedMessage object? Or a reference to another object from some other inherited class?



    To cast from an ThreadedMessage object to a GPSMessage object, a conversion is needed, and there's no such conversion possible (the ThreadedMessage class doesn't have a conversion operator, and GPSMessage doesn't have a suitable constructor).



    The only solution is to cast pointers or references to another pointer or reference. Like you do in the pointer example, or by doing e.g.



    const GPSMessage& message1 = static_cast<const GPSMessage&>(msg);



    If you really want to cast to a GPSMessage object, not reference or pointer, then the best solution is to use a conversion constructor:



    class GPSMessage : public ThreadedMessage

    public:
    ...
    explicit GPSMessage(const ThreadedMessage& tm)
    : GPSMessage(static_cast<const GPSMessage&>(tm)) // Invoke copy-constructor

    ...
    ;


    Using the copy-constructor is nice, but it requires that the tm argument really is a reference to a GPSMessage object. Otherwise it's invalid.




    Another solution is to make the classes polymorphic (simplest by making the destructors virtual) and use dynamic_cast to a pointer. If the result is a null pointer then msg wasn't a GPSMessage to begin with.






    share|improve this answer

























    • If one is very very sure of the type of msg, then const GPSMessage message1 = static_cast<const GPSMessage&>(msg); will work even without modifying GPSMessage.

      – StoryTeller
      Nov 14 '18 at 7:34











    • Thanks for the reminder about virtual destructors. It reminded me that the all of the classes were missing virtual destructors.

      – Fred
      Nov 14 '18 at 20:31










    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53294643%2fc2440-static-cast-cannot-convert-from-base-class-to-derived-class%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    These two casts have differing meaning.



    The first cast:



    const GPSMessage* message = static_cast<const GPSMessage*>(&msg); // Compiles fine


    This means that msg is actually a GPSMessage (or a derived) object. You ask the compiler to threat msg as a GPSMessage. No new object will be created, message will point to msg. If msg is not actually a GPSMessage (or derived), then this cast has Undefined Behavior.



    Btw, the following cast have the same meaning (casting to a reference):



    const GPSMessage& message = static_cast<const GPSMessage&>(msg); // same meaning, which results in a reference instead of a pointer


    About the second cast:



    const GPSMessage message1 = static_cast<const GPSMessage>(msg); // Error C2440


    This means, that you create a new object, message1 from msg. You should give a way to make this conversion possible. For example, you should create a constructor for GPSMessage which has a ThreadedMessage parameter:



    struct GPSMessage 
    GPSMessage(const ThreadedMessage &); // needed constructor
    ;


    Or create a conversion operator for ThreadedMessage to GPSMessage:



    struct ThreadedMessage 
    operator GPSMessage(); // conversion operator
    ;





    share|improve this answer



























      2














      These two casts have differing meaning.



      The first cast:



      const GPSMessage* message = static_cast<const GPSMessage*>(&msg); // Compiles fine


      This means that msg is actually a GPSMessage (or a derived) object. You ask the compiler to threat msg as a GPSMessage. No new object will be created, message will point to msg. If msg is not actually a GPSMessage (or derived), then this cast has Undefined Behavior.



      Btw, the following cast have the same meaning (casting to a reference):



      const GPSMessage& message = static_cast<const GPSMessage&>(msg); // same meaning, which results in a reference instead of a pointer


      About the second cast:



      const GPSMessage message1 = static_cast<const GPSMessage>(msg); // Error C2440


      This means, that you create a new object, message1 from msg. You should give a way to make this conversion possible. For example, you should create a constructor for GPSMessage which has a ThreadedMessage parameter:



      struct GPSMessage 
      GPSMessage(const ThreadedMessage &); // needed constructor
      ;


      Or create a conversion operator for ThreadedMessage to GPSMessage:



      struct ThreadedMessage 
      operator GPSMessage(); // conversion operator
      ;





      share|improve this answer

























        2












        2








        2







        These two casts have differing meaning.



        The first cast:



        const GPSMessage* message = static_cast<const GPSMessage*>(&msg); // Compiles fine


        This means that msg is actually a GPSMessage (or a derived) object. You ask the compiler to threat msg as a GPSMessage. No new object will be created, message will point to msg. If msg is not actually a GPSMessage (or derived), then this cast has Undefined Behavior.



        Btw, the following cast have the same meaning (casting to a reference):



        const GPSMessage& message = static_cast<const GPSMessage&>(msg); // same meaning, which results in a reference instead of a pointer


        About the second cast:



        const GPSMessage message1 = static_cast<const GPSMessage>(msg); // Error C2440


        This means, that you create a new object, message1 from msg. You should give a way to make this conversion possible. For example, you should create a constructor for GPSMessage which has a ThreadedMessage parameter:



        struct GPSMessage 
        GPSMessage(const ThreadedMessage &); // needed constructor
        ;


        Or create a conversion operator for ThreadedMessage to GPSMessage:



        struct ThreadedMessage 
        operator GPSMessage(); // conversion operator
        ;





        share|improve this answer













        These two casts have differing meaning.



        The first cast:



        const GPSMessage* message = static_cast<const GPSMessage*>(&msg); // Compiles fine


        This means that msg is actually a GPSMessage (or a derived) object. You ask the compiler to threat msg as a GPSMessage. No new object will be created, message will point to msg. If msg is not actually a GPSMessage (or derived), then this cast has Undefined Behavior.



        Btw, the following cast have the same meaning (casting to a reference):



        const GPSMessage& message = static_cast<const GPSMessage&>(msg); // same meaning, which results in a reference instead of a pointer


        About the second cast:



        const GPSMessage message1 = static_cast<const GPSMessage>(msg); // Error C2440


        This means, that you create a new object, message1 from msg. You should give a way to make this conversion possible. For example, you should create a constructor for GPSMessage which has a ThreadedMessage parameter:



        struct GPSMessage 
        GPSMessage(const ThreadedMessage &); // needed constructor
        ;


        Or create a conversion operator for ThreadedMessage to GPSMessage:



        struct ThreadedMessage 
        operator GPSMessage(); // conversion operator
        ;






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 7:39









        gezageza

        13.1k32776




        13.1k32776























            2














            In the function the compiler only knows that msg is a reference to a ThreadedMessage, it doesn't know what's really passed as an argument inside the function.



            Think about what happened if you passed a reference to an actual ThreadedMessage object? Or a reference to another object from some other inherited class?



            To cast from an ThreadedMessage object to a GPSMessage object, a conversion is needed, and there's no such conversion possible (the ThreadedMessage class doesn't have a conversion operator, and GPSMessage doesn't have a suitable constructor).



            The only solution is to cast pointers or references to another pointer or reference. Like you do in the pointer example, or by doing e.g.



            const GPSMessage& message1 = static_cast<const GPSMessage&>(msg);



            If you really want to cast to a GPSMessage object, not reference or pointer, then the best solution is to use a conversion constructor:



            class GPSMessage : public ThreadedMessage

            public:
            ...
            explicit GPSMessage(const ThreadedMessage& tm)
            : GPSMessage(static_cast<const GPSMessage&>(tm)) // Invoke copy-constructor

            ...
            ;


            Using the copy-constructor is nice, but it requires that the tm argument really is a reference to a GPSMessage object. Otherwise it's invalid.




            Another solution is to make the classes polymorphic (simplest by making the destructors virtual) and use dynamic_cast to a pointer. If the result is a null pointer then msg wasn't a GPSMessage to begin with.






            share|improve this answer

























            • If one is very very sure of the type of msg, then const GPSMessage message1 = static_cast<const GPSMessage&>(msg); will work even without modifying GPSMessage.

              – StoryTeller
              Nov 14 '18 at 7:34











            • Thanks for the reminder about virtual destructors. It reminded me that the all of the classes were missing virtual destructors.

              – Fred
              Nov 14 '18 at 20:31















            2














            In the function the compiler only knows that msg is a reference to a ThreadedMessage, it doesn't know what's really passed as an argument inside the function.



            Think about what happened if you passed a reference to an actual ThreadedMessage object? Or a reference to another object from some other inherited class?



            To cast from an ThreadedMessage object to a GPSMessage object, a conversion is needed, and there's no such conversion possible (the ThreadedMessage class doesn't have a conversion operator, and GPSMessage doesn't have a suitable constructor).



            The only solution is to cast pointers or references to another pointer or reference. Like you do in the pointer example, or by doing e.g.



            const GPSMessage& message1 = static_cast<const GPSMessage&>(msg);



            If you really want to cast to a GPSMessage object, not reference or pointer, then the best solution is to use a conversion constructor:



            class GPSMessage : public ThreadedMessage

            public:
            ...
            explicit GPSMessage(const ThreadedMessage& tm)
            : GPSMessage(static_cast<const GPSMessage&>(tm)) // Invoke copy-constructor

            ...
            ;


            Using the copy-constructor is nice, but it requires that the tm argument really is a reference to a GPSMessage object. Otherwise it's invalid.




            Another solution is to make the classes polymorphic (simplest by making the destructors virtual) and use dynamic_cast to a pointer. If the result is a null pointer then msg wasn't a GPSMessage to begin with.






            share|improve this answer

























            • If one is very very sure of the type of msg, then const GPSMessage message1 = static_cast<const GPSMessage&>(msg); will work even without modifying GPSMessage.

              – StoryTeller
              Nov 14 '18 at 7:34











            • Thanks for the reminder about virtual destructors. It reminded me that the all of the classes were missing virtual destructors.

              – Fred
              Nov 14 '18 at 20:31













            2












            2








            2







            In the function the compiler only knows that msg is a reference to a ThreadedMessage, it doesn't know what's really passed as an argument inside the function.



            Think about what happened if you passed a reference to an actual ThreadedMessage object? Or a reference to another object from some other inherited class?



            To cast from an ThreadedMessage object to a GPSMessage object, a conversion is needed, and there's no such conversion possible (the ThreadedMessage class doesn't have a conversion operator, and GPSMessage doesn't have a suitable constructor).



            The only solution is to cast pointers or references to another pointer or reference. Like you do in the pointer example, or by doing e.g.



            const GPSMessage& message1 = static_cast<const GPSMessage&>(msg);



            If you really want to cast to a GPSMessage object, not reference or pointer, then the best solution is to use a conversion constructor:



            class GPSMessage : public ThreadedMessage

            public:
            ...
            explicit GPSMessage(const ThreadedMessage& tm)
            : GPSMessage(static_cast<const GPSMessage&>(tm)) // Invoke copy-constructor

            ...
            ;


            Using the copy-constructor is nice, but it requires that the tm argument really is a reference to a GPSMessage object. Otherwise it's invalid.




            Another solution is to make the classes polymorphic (simplest by making the destructors virtual) and use dynamic_cast to a pointer. If the result is a null pointer then msg wasn't a GPSMessage to begin with.






            share|improve this answer















            In the function the compiler only knows that msg is a reference to a ThreadedMessage, it doesn't know what's really passed as an argument inside the function.



            Think about what happened if you passed a reference to an actual ThreadedMessage object? Or a reference to another object from some other inherited class?



            To cast from an ThreadedMessage object to a GPSMessage object, a conversion is needed, and there's no such conversion possible (the ThreadedMessage class doesn't have a conversion operator, and GPSMessage doesn't have a suitable constructor).



            The only solution is to cast pointers or references to another pointer or reference. Like you do in the pointer example, or by doing e.g.



            const GPSMessage& message1 = static_cast<const GPSMessage&>(msg);



            If you really want to cast to a GPSMessage object, not reference or pointer, then the best solution is to use a conversion constructor:



            class GPSMessage : public ThreadedMessage

            public:
            ...
            explicit GPSMessage(const ThreadedMessage& tm)
            : GPSMessage(static_cast<const GPSMessage&>(tm)) // Invoke copy-constructor

            ...
            ;


            Using the copy-constructor is nice, but it requires that the tm argument really is a reference to a GPSMessage object. Otherwise it's invalid.




            Another solution is to make the classes polymorphic (simplest by making the destructors virtual) and use dynamic_cast to a pointer. If the result is a null pointer then msg wasn't a GPSMessage to begin with.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 14 '18 at 7:37

























            answered Nov 14 '18 at 7:03









            Some programmer dudeSome programmer dude

            302k25264424




            302k25264424












            • If one is very very sure of the type of msg, then const GPSMessage message1 = static_cast<const GPSMessage&>(msg); will work even without modifying GPSMessage.

              – StoryTeller
              Nov 14 '18 at 7:34











            • Thanks for the reminder about virtual destructors. It reminded me that the all of the classes were missing virtual destructors.

              – Fred
              Nov 14 '18 at 20:31

















            • If one is very very sure of the type of msg, then const GPSMessage message1 = static_cast<const GPSMessage&>(msg); will work even without modifying GPSMessage.

              – StoryTeller
              Nov 14 '18 at 7:34











            • Thanks for the reminder about virtual destructors. It reminded me that the all of the classes were missing virtual destructors.

              – Fred
              Nov 14 '18 at 20:31
















            If one is very very sure of the type of msg, then const GPSMessage message1 = static_cast<const GPSMessage&>(msg); will work even without modifying GPSMessage.

            – StoryTeller
            Nov 14 '18 at 7:34





            If one is very very sure of the type of msg, then const GPSMessage message1 = static_cast<const GPSMessage&>(msg); will work even without modifying GPSMessage.

            – StoryTeller
            Nov 14 '18 at 7:34













            Thanks for the reminder about virtual destructors. It reminded me that the all of the classes were missing virtual destructors.

            – Fred
            Nov 14 '18 at 20:31





            Thanks for the reminder about virtual destructors. It reminded me that the all of the classes were missing virtual destructors.

            – Fred
            Nov 14 '18 at 20:31

















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53294643%2fc2440-static-cast-cannot-convert-from-base-class-to-derived-class%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo