473,394 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 14201
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
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...
22
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...
8
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...
15
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...
10
by: Ioannis Vranos | last post by:
May someone explain why does this compile? class HiddenSealBaseClass { public: HiddenSealBaseClass() { } }; class Sealed: virtual HiddenSealBaseClass
3
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...
5
by: Martin Jørgensen | last post by:
Hi, Consider this code: --- beginning of code --- #include <iostream> using namespace std; class Child{ public:
46
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
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:...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.