473,725 Members | 2,251 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++ Struct inheritance against class inheritance

When a class derives from a class,
You can use a pointer to the parent class to delete the instance of
child
only when a virtual destructor declared in the parent.

class Parent
{
virtual ~Parent(); // virtual destructor
}

class Child : public Parent
{
}

Parent * instance = new Child();
delete instance;

=============== =============== =============== ==

But is it the same when parent is a struct?

struct StructParent {
}

class Child : public StructParent {
}

StructParent * instance = new Child();
delete instance;

Does this mean that I should decleare a virtual destructor in
StructParent
to provide correct information about the parent and a safe delete
operation?
Or I can only write codes like:
Child * instance = new Child();
delete instance;

I'm a little confused.

Can I say that,
if I ensure that I only use pointer to the Child (bug not the
Parent's) and
perform delete operation on it, plus there is no virtual method in
both Parent and Child,
I can define no virtual destructor in struct inheritance and class
inheritance.

Thank you.
Jul 21 '08 #1
1 14228
johnsonlau wrote:
When a class derives from a class,
You can use a pointer to the parent class to delete the instance of
child
only when a virtual destructor declared in the parent.

class Parent
{
virtual ~Parent(); // virtual destructor
}

class Child : public Parent
{
}

Parent * instance = new Child();
delete instance;

=============== =============== =============== ==

But is it the same when parent is a struct?
Yes, it is the same.
struct StructParent {
}

class Child : public StructParent {
}

StructParent * instance = new Child();
delete instance;
This is undefined behavior as you suspected.

Does this mean that I should decleare a virtual destructor in
StructParent
to provide correct information about the parent and a safe delete
operation?
That is one way.

Or I can only write codes like:
Child * instance = new Child();
delete instance;
That is fine, too.

I'm a little confused.

Can I say that,
if I ensure that I only use pointer to the Child (bug not the
Parent's) and
perform delete operation on it, plus there is no virtual method in
both Parent and Child,
I can define no virtual destructor in struct inheritance and class
inheritance.
Huh? Of course you can define a virtual destructor. However, in the case you
described, you don't have to.

What you need to keep in mind is that a virtual destructor is needed
whenever you delete an object of derived type through a pointer to a base.
It does not matter whether the type its a struct or a class nor whether it
has other virtual methods or not.

BTW: structs and classes in C++ only differ with regard to the default
access; structs are public by default and classes have private access by
default.
Best

Kai-Uwe Bux
Jul 21 '08 #2

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

Similar topics

3
5829
by: Cgacc20 | last post by:
I have a c struct from old code that cannot be modified and I am trying to write a wrapper C++ class around it. This class is often passed as a pointer to some c functions of a library and I wanted to keep my new class transparent and compatible with those functions. That is, when using the code, I should be able to do either: oldVector xyz; function( &xyz ); myVector xyz; function( &xyz );
22
5833
by: Marcin Vorbrodt | last post by:
Taken out of C++ In a Nutshell book... Example 7-18: Using an abstract classes as interface specification. struct Runnable { virtual void run() = 0; }; struct Hashable { virtual size_t hash() = 0; };
8
5971
by: slurper | last post by:
hi, i'm studying some stl. i saw the pair implementation in a header-file but what i wonder is if a struct can have constructors as it seems in following snippet from the stl library. the snippets i wonder about are indicated with --------> : i thought a struct doesn't have constructors (but here there are four, actually three) template <class _T1, class _T2> struct pair {
15
9070
by: Steven T. Hatton | last post by:
The following may strike many of you as just plain silly, but it represents the kind of delelima I find myself in when trying to make a design decision. This really is a toy project written for the purpose of learning to work with C++. It therefore makes some sense for me to give the situation the amount of consideration presented below. To be quite honest, I'm amazed at the amount there is to say about such a seemingly simple...
10
2487
by: Ioannis Vranos | last post by:
May someone explain why does this compile? class HiddenSealBaseClass { public: HiddenSealBaseClass() { } }; class Sealed: virtual HiddenSealBaseClass
3
14100
by: Boaz Ben-Porat | last post by:
I Know, ... a struct can not be inherited. ** Is there any tricky way to extend a struct ? ** I have to implement a component that represent a date (without time) that has a small number of functions over those of DataTime type. My first thougt was : "Inherit from DateTime and implement those functions". Then I found out that DateTime is a struct, not a class, so it can`t be done. For example, The new type should implement an...
5
4118
by: Martin Jørgensen | last post by:
Hi, Consider this code: --- beginning of code --- #include <iostream> using namespace std; class Child{ public:
46
2502
by: clintonG | last post by:
Documentation tells me how but not when, why or where... <%= Clinton Gallagher http://msdn2.microsoft.com/en-us/library/saxz13w4(VS.80).aspx
3
2900
by: Leo Seccia | last post by:
Hello everyone, I have a c# project with a sql server database. I have a number of lookup tables in my database which I successfully managed to import into my LINQ dataclasses. eg. Table: tlkpColor (PK) tlkpColorID
0
8889
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
9401
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
9257
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
9179
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
9116
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...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4519
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...
1
3228
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.