is this valid c++?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Simon

    #1

    is this valid c++?

    Hi,

    If I have the following :

    struct sA
    {
    double A1;
    double A2;
    };

    struct sB
    {
    double B1;
    long B2;
    };

    struct sC : sA, sB
    {
    double C1;
    double C2;
    };

    //
    // main app
    sC myC = ... // set some values;
    sA myA = (sA)myC;
    sB myB = (sB)myB;

    //
    //

    When I do it on my compiler, VS2009 it works.

    In other word the value of myA has the values from myC simply by casting.
    And the same for myB getting the right values from myC by casting.

    But I doubt there are any rules in the standard that say that when you cast
    like that you must copy the right values.

    If this is not 'standard' what would be the better way of getting sA and sB
    from sC

    Many thanks

    Simon


  • peter koch

    #2
    Re: is this valid c++?

    On 19 Sep., 20:00, "Simon" <spambuc...@exa mple.comwrote:
    Hi,
    >
    If I have the following :
    >
    struct sA
    {
      double A1;
      double A2;
    >
    };
    >
    struct sB
    {
      double B1;
      long B2;
    >
    };
    >
    struct sC :  sA, sB
    {
      double C1;
      double C2;
    >
    };
    >
    //
    // main app
    sC myC = ... // set some values;
    sA myA = (sA)myC;
    sB myB = (sB)myB;
    >
    //
    //
    >
    When I do it on my compiler, VS2009 it works.
    >
    In other word the value of myA has the values from myC simply by casting.
    And the same for myB getting the right values from myC by casting.
    You don't need the cast - remove it.
    >
    But I doubt there are any rules in the standard that say that when you cast
    like that you must copy the right values.
    There is not, but there are good rules that tell you to avoid casts
    and - equally important - "never" to use C-style casts. C++ has
    defined safer and better casts - use those instead.

    /Peter

    Comment

    • James Kanze

      #3
      Re: is this valid c++?

      On Sep 19, 8:00 pm, "Simon" <spambuc...@exa mple.comwrote:
      If I have the following :
      struct sA
      {
      double A1;
      double A2;
      };
      struct sB
      {
      double B1;
      long B2;
      };
      struct sC : sA, sB
      {
      double C1;
      double C2;
      };
      //
      // main app
      sC myC = ... // set some values;
      sA myA = (sA)myC;
      sB myB = (sB)myB;
      //
      //
      When I do it on my compiler, VS2009 it works.
      One would hope so.
      In other word the value of myA has the values from myC simply
      by casting. And the same for myB getting the right values
      from myC by casting.
      You don't even need the casts. An sC isA sA and an sB.
      But I doubt there are any rules in the standard that say that
      when you cast like that you must copy the right values.
      Of course there are. It's more or less one of the basics of
      inheritance.

      --
      James Kanze (GABI Software) email:james.kan ze@gmail.com
      Conseils en informatique orientée objet/
      Beratung in objektorientier ter Datenverarbeitu ng
      9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

      Comment

      • blargg

        #4
        Re: is this valid c++?

        In article
        <ef909c34-f805-4b42-839b-81f80cafc147@z6 6g2000hsc.googl egroups.com>, James
        Kanze <james.kanze@gm ail.comwrote:
        On Sep 19, 8:00 pm, "Simon" <spambuc...@exa mple.comwrote:
        If I have the following :
        >
        struct sA
        {
        double A1;
        double A2;
        };
        >
        struct sB
        {
        double B1;
        long B2;
        };
        >
        struct sC : sA, sB
        {
        double C1;
        double C2;
        };
        >
        //
        // main app
        sC myC = ... // set some values;
        sA myA = (sA)myC;
        sB myB = (sB)myB;
        >
        //
        //
        >
        When I do it on my compiler, VS2009 it works.
        >
        One would hope so.
        Except that the last line invokes undefined behavior by assigning the
        uninitialized myB to itself.

        Comment

        • Simon

          #5
          Re: is this valid c++?

          >>
          //
          //
          >>
          When I do it on my compiler, VS2009 it works.
          >>
          >One would hope so.
          >
          Except that the last line invokes undefined behavior by assigning the
          uninitialized myB to itself.
          oops, that was a typo
          it should say

          ....
          sC myC = ... // set some values;
          sA myA = (sA)myC;
          sB myB = (sB)myC;

          Thanks

          Simon

          Comment

          Working...