classes vs structs.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bilgehan.Balban@gmail.com

    #1

    classes vs structs.

    Hi,

    I am currently brushing up my c++ knowledge and I would like to ask you
    about the differences between classes and C structs, in the
    function/method perspective.

    1) Is it correct to say that, a structure definition that includes
    function pointers only defines the function prototypes to be used with
    them, but not the actual implementations , whereas in C++, member
    functions cannot be changed *unless* virtual functions are used, or the
    original class is extended with a new one that defines
    the same functions with different implementations ?

    2) Once defined, accessing member functions of a class in C++ has less
    overhead thanks to name mangling, compared to the extra pointer
    dereference overhead
    in C structs with function pointers?

    3) It is commonly said that virtual functions reduce performance.
    AFAIK, the overhead of virtual functions compared to a function pointer
    in a C struct is, only one extra pointer dereference and an addition.
    E.g. one dereference to access vtab, and one offset addition and
    dereference to access the function itself.

    Is it even more than that, otherwise why does virtual functions have
    such reputation to reduce performance where C structs with function
    pointers are not that much better?

    4) Also, isn't there the possibility to optimise the dereference in C
    structs, at least if the function pointer is declared `const', i.e. not
    to change after the initialisation. The compiler might just recognise
    the const initialisation and replace it with a `branch' to the actual
    function? Perhaps not a possible option with virtual functions due to
    dynamic binding?

    5) In a C struct on a 32-bit machine, the size of a struct is the sum
    of all fields + maybe some padding; the function pointers take up as
    much as a word. In C++ what is the size of a class, perhaps it excludes
    sizes of member methods? Is it only the fields? What about static
    storage? Does a static member count in every instance of the class? And
    const members? Is it implementation defined where consts are stored? If
    I wanted to memcpy a class, how much space should I spare? Perhaps it
    is undefined behaviour if one manually copied a class and expected to
    access its fields as normal?

    Many thanks,
    Bahadir

  • Victor Bazarov

    #2
    Re: classes vs structs.

    Bilgehan.Balban @gmail.com wrote:[color=blue]
    > I am currently brushing up my c++ knowledge and I would like to ask you
    > about the differences between classes and C structs, in the
    > function/method perspective.
    >
    > 1) Is it correct to say that, a structure definition that includes
    > function pointers only defines the function prototypes to be used with
    > them,[/color]

    Huh?
    [color=blue]
    > but not the actual implementations , whereas in C++, member
    > functions cannot be changed *unless* virtual functions are used, or the
    > original class is extended with a new one that defines
    > the same functions with different implementations ?[/color]

    No. Incorrect on several levels.
    [color=blue]
    > 2) Once defined, accessing member functions of a class in C++ has less
    > overhead thanks to name mangling, compared to the extra pointer
    > dereference overhead
    > in C structs with function pointers?[/color]

    Maybe. Only experimentation can actually show. And then after that, it
    still needs to be proven to be of any importance.
    [color=blue]
    > 3) It is commonly said that virtual functions reduce performance.
    > AFAIK, the overhead of virtual functions compared to a function pointer
    > in a C struct is, only one extra pointer dereference and an addition.
    > E.g. one dereference to access vtab, and one offset addition and
    > dereference to access the function itself.[/color]

    I don't think there is dereference to access vtab. I don't think the
    paragraph above makes sense.
    [color=blue]
    > Is it even more than that, otherwise why does virtual functions have
    > such reputation to reduce performance where C structs with function
    > pointers are not that much better?[/color]

    Because some people like looking for flaws. Did you know that cars have
    the reputation of killing their occupants?
    [color=blue]
    > 4) Also, isn't there the possibility to optimise the dereference in C
    > structs, at least if the function pointer is declared `const', i.e. not
    > to change after the initialisation. The compiler might just recognise
    > the const initialisation and replace it with a `branch' to the actual
    > function? Perhaps not a possible option with virtual functions due to
    > dynamic binding?[/color]

    Actually, if the call to any function even declared 'virtual' is made
    non-polymorphically , there is no overhead.
    [color=blue]
    > 5) In a C struct on a 32-bit machine, the size of a struct is the sum
    > of all fields + maybe some padding; the function pointers take up as
    > much as a word. In C++ what is the size of a class, perhaps it excludes
    > sizes of member methods?[/color]

    Usually member functions do not contribute to the size of the object.
    The most common implementation of virtual function mechanism adds one
    pointer to an object of the class with any number of virtual functions.
    [color=blue]
    > Is it only the fields? What about static
    > storage? Does a static member count in every instance of the class?[/color]

    No.
    [color=blue]
    > And
    > const members?[/color]

    Depends on the members.
    [color=blue]
    > Is it implementation defined where consts are stored? If
    > I wanted to memcpy a class, how much space should I spare? Perhaps it
    > is undefined behaviour if one manually copied a class and expected to
    > access its fields as normal?[/color]

    I think you need to get and read "Inside the C++ Object Model" by Lippman.

    V
    --
    Please remove capital As from my address when replying by mail

    Comment

    • Ed Weir \(ComCast\)

      #3
      Re: classes vs structs.

      <Bilgehan.Balba n@gmail.com> wrote in message
      news:1139323671 .917494.133700@ f14g2000cwb.goo glegroups.com.. .
      | Hi,
      |
      | I am currently brushing up my c++ knowledge and I would like to ask you
      | about the differences between classes and C structs, in the
      | function/method perspective.
      |
      | 1) Is it correct to say that, a structure definition that includes
      | function pointers only defines the function prototypes to be used with
      | them, but not the actual implementations , whereas in C++, member
      | functions cannot be changed *unless* virtual functions are used, or the
      | original class is extended with a new one that defines
      | the same functions with different implementations ?
      |
      | 2) Once defined, accessing member functions of a class in C++ has less
      | overhead thanks to name mangling, compared to the extra pointer
      | dereference overhead
      | in C structs with function pointers?
      |
      | 3) It is commonly said that virtual functions reduce performance.
      | AFAIK, the overhead of virtual functions compared to a function pointer
      | in a C struct is, only one extra pointer dereference and an addition.
      | E.g. one dereference to access vtab, and one offset addition and
      | dereference to access the function itself.
      |
      | Is it even more than that, otherwise why does virtual functions have
      | such reputation to reduce performance where C structs with function
      | pointers are not that much better?
      |
      | 4) Also, isn't there the possibility to optimise the dereference in C
      | structs, at least if the function pointer is declared `const', i.e. not
      | to change after the initialisation. The compiler might just recognise
      | the const initialisation and replace it with a `branch' to the actual
      | function? Perhaps not a possible option with virtual functions due to
      | dynamic binding?
      |
      | 5) In a C struct on a 32-bit machine, the size of a struct is the sum
      | of all fields + maybe some padding; the function pointers take up as
      | much as a word. In C++ what is the size of a class, perhaps it excludes
      | sizes of member methods? Is it only the fields? What about static
      | storage? Does a static member count in every instance of the class? And
      | const members? Is it implementation defined where consts are stored? If
      | I wanted to memcpy a class, how much space should I spare? Perhaps it
      | is undefined behaviour if one manually copied a class and expected to
      | access its fields as normal?
      |
      | Many thanks,
      | Bahadir

      There is very little difference between classes and structs in C++.
      Anything you can do in a class you can do in a struct, and vice versa.
      Structs are C baggage carried over to C++ and replaced by the class. The
      only discernable difference is that ALL members of a struct are public by
      default, and ALL members of a class are private by default. I still use the
      typedef struct style in C++ to point out to the reader that I intend to use
      this as a data container only, even though it might have some intelligence
      built in about its data. Note that it is considered very bad style to
      polymorph structs - you should use classes for that.

      Cheers

      Comment

      • Roland Pibinger

        #4
        Re: classes vs structs.

        On Tue, 7 Feb 2006 09:22:44 -0800, "Ed Weir \(ComCast\)"
        <Anon@Maus.du h> wrote:[color=blue]
        >There is very little difference between classes and structs in C++.
        >Anything you can do in a class you can do in a struct, and vice versa.[/color]

        The OP's question possibly is:

        struct S {
        void (*foo) (int);
        };

        vs.

        class C1 {
        public:
        void foo (int);
        };

        vs.

        class C2 {
        public:
        virtual void foo (int);
        };

        Comment

        • Victor Bazarov

          #5
          Re: classes vs structs.

          Roland Pibinger wrote:[color=blue]
          > On Tue, 7 Feb 2006 09:22:44 -0800, "Ed Weir \(ComCast\)"
          > <Anon@Maus.du h> wrote:
          >[color=green]
          >>There is very little difference between classes and structs in C++.
          >>Anything you can do in a class you can do in a struct, and vice versa.[/color]
          >
          >
          > The OP's question possibly is:
          >
          > struct S {
          > void (*foo) (int);[/color]

          Actually, you'd probably need to make it

          void (*foo) (struct S*, int);

          to match C++ functionality. After all, what if 'S' has data members?
          [color=blue]
          > };
          >
          > vs.
          >
          > class C1 {
          > public:
          > void foo (int);
          > };
          >
          > vs.
          >
          > class C2 {
          > public:
          > virtual void foo (int);
          > };
          >[/color]

          V
          --
          Please remove capital As from my address when replying by mail

          Comment

          • Roland Pibinger

            #6
            Re: classes vs structs.

            On Tue, 07 Feb 2006 16:18:02 -0500, Victor Bazarov
            <v.Abazarov@com Acast.net> wrote:
            [color=blue]
            >Roland Pibinger wrote:[color=green]
            >> The OP's question possibly is:
            >>
            >> struct S {
            >> void (*foo) (int);[/color]
            >
            >Actually, you'd probably need to make it
            >
            > void (*foo) (struct S*, int);
            >
            >to match C++ functionality. After all, what if 'S' has data members?[/color]

            You are right!

            Comment

            Working...