473,608 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Feb 7 '06 #1
5 2903
Bi************* @gmail.com wrote:
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,
Huh?
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 ?
No. Incorrect on several levels.
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?
Maybe. Only experimentation can actually show. And then after that, it
still needs to be proven to be of any importance.
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.
I don't think there is dereference to access vtab. I don't think the
paragraph above makes sense.
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?
Because some people like looking for flaws. Did you know that cars have
the reputation of killing their occupants?
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?
Actually, if the call to any function even declared 'virtual' is made
non-polymorphically , there is no overhead.
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?
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.
Is it only the fields? What about static
storage? Does a static member count in every instance of the class?
No.
And
const members?
Depends on the 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?


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
Feb 7 '06 #2
<Bi************ *@gmail.com> wrote in message
news:11******** **************@ 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

Feb 7 '06 #3
On Tue, 7 Feb 2006 09:22:44 -0800, "Ed Weir \(ComCast\)"
<An**@Maus.du h> wrote:
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.


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);
};

Feb 7 '06 #4
Roland Pibinger wrote:
On Tue, 7 Feb 2006 09:22:44 -0800, "Ed Weir \(ComCast\)"
<An**@Maus.du h> wrote:
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.

The OP's question possibly is:

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


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?
};

vs.

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

vs.

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


V
--
Please remove capital As from my address when replying by mail
Feb 7 '06 #5
On Tue, 07 Feb 2006 16:18:02 -0500, Victor Bazarov
<v.********@com Acast.net> wrote:
Roland Pibinger wrote:
The OP's question possibly is:

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


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?


You are right!
Feb 7 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1578
by: Brad Tilley | last post by:
I don't understand classes very well... maybe some day. Is it just me or are they supposed to be difficult to understand? They make my head hurt. Anyway, because I don't understand classes well, I avoid using them. However, many modules in the standard library are implemented as classes: sgmllib, HTMLParser, etc. Is it possible to use these modules without getting into OO programming and inheritance and all the other lofty, theoretical...
14
3781
by: Pratts | last post by:
I am a new one who have joined u plz try to help me bcoz i could not find ny sutiable answer foer this Question Qus>>why do we need classes when structures provide similar functionality??
4
4271
by: Bill | last post by:
I would like to create a static array of classes (or structs) to be used in populating name/value pairs in various WebForm drop down list boxes, but am not quite sure of the construct (or rather to use structs instead of classes in the array insofar as structs vs. classes appears to be controversial in C# -- with some recommending avoiding structs altogether). It needs to be an array something like this: struct NoteValue {
4
1966
by: Christopher Ireland | last post by:
Hi -- I'm trying to find an example of a nested class implemented within the .NET Framework itself but without much success. I don't suppose that anybody knows of such an example off the top of their head, do they? Many thanks! -- Best Regards,
2
1362
by: Kiran A K | last post by:
Hi, can anyone give specific examples where structs are preferred to classes?
7
2244
by: Markus Svilans | last post by:
Hi, What is the difference between having a struct with constructors and methods versus a class? In many C++ examples online I have seen code similar to this: struct Animal { Animal()
5
1616
by: giddy | last post by:
hi , i'm a C / C# programmer .. have'nt done C++, in C# .. . object instances of classes ( TextBox txt = new TextBox();) are reference types. Structs on the other hand are value types. In C++ i knw there are a few difference between classes and structs but i need to know if there are value or refrence types.
29
2758
by: Dom | last post by:
I'm really confused by the difference between a Struct and a Class? Sometimes, I want just a group of fields to go together. A Class without methods seems wrong, in that it carries too much overhead (I think). A Struct seems more appropriate. At least it is what I would have used in other languages. But since a Struct *can* hold methods, I wander if I am saving anything. If not, why use it?
19
2545
by: desktop | last post by:
There is a lot of info on this topic on google. But in Bjarne Stroustrup 's book page 225 he writes: "The declaration of Date in the previous subsection (declared as a struct) provides a set of functions for manipulating a Date. However, it does not specify that those functions should be the only ones to depend directly on Date ’s representation and the only ones to directly access objects of class Date . This restriction can be...
0
8063
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8498
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8478
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8152
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8341
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3962
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4025
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1331
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.