How to get the correct size of inherit class?

Discussion in 'C++' started by Zhou Fan, Mar 23, 2006.

  1. Zhou Fan

    Zhou Fan Guest

    Here is asserts:
    BOOST_CHECK(sizeof(base_config) != sizeof(mine_config));
    BOOST_CHECK_EQUAL(sizeof(mine_config), mine_config().size());

    Here is implement:

    class base_config
    {
    public:
    virtual unsigned size() const { return sizeof(*this); }
    };

    class mine_config :
    public base_config
    {
    public:
    int x;
    };

    Here is result:
    error in "testSize": check sizeof(mine_config) == mine_config().size()
    failed [8 != 4]

    Why wrong? How to do it?
    Thanks a lot.


    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
    Zhou Fan, Mar 23, 2006
    #1
    1. Advertising

  2. Zhou Fan

    Rolf Magnus Guest

    Zhou Fan wrote:

    > Here is asserts:
    > BOOST_CHECK(sizeof(base_config) != sizeof(mine_config));
    > BOOST_CHECK_EQUAL(sizeof(mine_config), mine_config().size());
    >
    > Here is implement:
    >
    > class base_config
    > {
    > public:
    > virtual unsigned size() const { return sizeof(*this); }
    > };
    >
    > class mine_config :
    > public base_config
    > {
    > public:
    > int x;
    > };
    >
    > Here is result:
    > error in "testSize": check sizeof(mine_config) == mine_config().size()
    > failed [8 != 4]
    >
    > Why wrong?


    Well, what did you expect? Since base_config::size() is not overriden in
    mine_config, that base class's version is called. And in base_config,
    *this
    is of course of type base_config.

    > How to do it?


    Override size() in every class derived from base_config.


    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
    Rolf Magnus, Mar 23, 2006
    #2
    1. Advertising

  3. Zhou Fan

    Vijay Guest

    See. size of mine_config = size of virtual pointer, as u have declare
    function size() as virtual, and size of int x.

    And class base_config, having only virtual pointer.
    Thats why, size of mine_config = 4+ 4, and size of base_config = 4.

    U have to remove either virtual keyword or that Assert to make it error
    free

    -Vijay Mishra


    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
    Vijay, Mar 23, 2006
    #3
  4. Zhou Fan

    R.F. Pels Guest

    Zhou Fan wrote:

    > virtual unsigned size() const { return sizeof(*this); }
    > Why wrong? How to do it?


    What is the type of 'this' at that particular moment?

    --
    ruurd

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
    R.F. Pels, Mar 23, 2006
    #4
  5. * Zhou Fan:
    > Here is asserts:
    > BOOST_CHECK(sizeof(base_config) != sizeof(mine_config));
    > BOOST_CHECK_EQUAL(sizeof(mine_config), mine_config().size());
    >
    > Here is implement:
    >
    > class base_config
    > {
    > public:
    > virtual unsigned size() const { return sizeof(*this); }
    > };
    >
    > class mine_config :
    > public base_config
    > {
    > public:
    > int x;
    > };
    >
    > Here is result:
    > error in "testSize": check sizeof(mine_config) == mine_config().size()
    > failed [8 != 4]
    >
    > Why wrong?


    Well, it's actually correct. But if you mean, why doesn't
    base_config::size return sizeof(mine_config) when called on a
    mine_config object, that's because sizeof is a compile time expression.


    > How to do it?


    Override size in your mine_config class.

    > Thanks a lot.


    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
    Alf P. Steinbach, Mar 23, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DBak
    Replies:
    11
    Views:
    1,002
  2. jonnytheclown

    Correct way to inherit from 3rd party class

    jonnytheclown, Feb 21, 2005, in forum: Perl Misc
    Replies:
    6
    Views:
    138
    Peter Scott
    Feb 22, 2005
  3. Peter Otten
    Replies:
    0
    Views:
    132
    Peter Otten
    Mar 2, 2013
  4. Ian Kelly
    Replies:
    0
    Views:
    113
    Ian Kelly
    Mar 2, 2013
  5. Ian Kelly
    Replies:
    0
    Views:
    115
    Ian Kelly
    Mar 2, 2013
Loading...

Share This Page