473,396 Members | 1,990 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,396 software developers and data experts.

any potential multi-heritance fault?

Hi all,

Could you tell me any potential fault in the following codes if I run
them on UNIX? BTW, class B could contain a lot of data.

#include <iostream.h>

class A{
public:
A(){}
~A(){}

int size;
int a;
};

class B : public A{
public:
B(){}
~B(){}
int b;
};

class C{
public:
C(){c = 100;}
~C(){}
int c;
};

class D : public C, public A{
public:
D():C(){}
~D(){}
};

int main()
{
B *bp = new B();
bp->b = 10;
bp->size = sizeof(B);

int size = sizeof(C) + bp->size;

D *dp;
dp = (D*) new char[size];
new (dp)D();

memcpy((A*)dp, bp, bp->size);

cout << "b:" << dp->size << endl;

delete []((char*)dp);
delete bp;
}

Best Regards,

Patricia

Sep 20 '05 #1
1 1264
Patricia wrote:
Hi all,

Could you tell me any potential fault in the following codes if I run
them on UNIX?
The platform should be irrelevant.
BTW, class B could contain a lot of data.
I trust that your example below actually demonstrates your question,
then. If the actual code differs too much in principle, then our
answers won't help.
#include <iostream.h>
#include <iostream> // iostream.h is deprecated
#include <new> // for placement new used in main
#include <cstring> // for memcpy
using namespace std;

class A{
public:
A(){}
~A(){}

int size;
int a;
};
Generally it's a poor design to have public data, but I suspect this is
only for purposes of this example.

class B : public A{
public:
B(){}
~B(){}
int b;
};

class C{
public:
C(){c = 100;}
// See http://www.parashift.com/c++-faq-lit....html#faq-10.6
C() : c(100) {}
~C(){}
int c;
};

class D : public C, public A{
public:
D():C(){}
~D(){}
};

int main()
{
B *bp = new B();
Prefer attaching a smart pointer (e.g. std::auto_ptr,
boost::shared_ptr, etc.) to any object allocated with new. It provides
exception safety, ease of use (i.e., you don't have to remember to
delete it), and no performance overhead.
bp->b = 10;
bp->size = sizeof(B);

int size = sizeof(C) + bp->size;

D *dp;
dp = (D*) new char[size];
new (dp)D();

memcpy((A*)dp, bp, bp->size);
Bad bad bad. Don't do this. Your objects should define copy
constructors or assignment operators if you want to convert between
them. The appearance of memcpy in C++ programs should set off alarms
and raise danger flags.

cout << "b:" << dp->size << endl;

You have a destructor for D, which I presume means you want to use it,
but since you used placement new, you must explicitly call the
destructor before you delete its allocated memory (see
http://www.parashift.com/c++-faq-lit...html#faq-11.10 et al.):

dp->~D();
delete []((char*)dp);
delete bp;
return 0;
}

Best Regards,

Patricia


Cheers! --M

Sep 20 '05 #2

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

Similar topics

3
by: Peter Sommerlad | last post by:
Hi, just a few questions: A) Do you know of any C++ Application Server frameworks, i.e. libraries, that make it easy to develop (Web) server applications? B) Would there be interest in such a...
27
by: Mark | last post by:
I'm curious to get feedback regarding the potential SVG has in performing the same functionality as PDF for fixing documents. Thanks. Mark
12
by: * ProteanThread * | last post by:
but depends upon the clique: ...
0
by: frankenberry | last post by:
I have multi-page tiff files. I need to extract individual frames from the multi-page tiffs and save them as single-page tiffs. 95% of the time I receive multi-page tiffs containing 1 or more black...
6
by: cody | last post by:
What are multi file assemblies good for? What are the advantages of using multiple assemblies (A.DLL+B.DLL) vs. a single multi file assembly (A.DLL+A.NETMODULE)?
0
by: Benoit Martin | last post by:
Hi, I've had my application going back to desktop randomly when executing ShowDialog commands. I posted to this list and was asked to post code but unfortunately I cannot reproduce this problem...
5
by: bobwansink | last post by:
Hi, I'm relatively new to programming and I would like to create a C++ multi user program. It's for a project for school. This means I will have to write a paper about the theory too. Does anyone...
0
by: Josh Berkus | last post by:
Folks, While debugging a wireless card, I came across this interesting bit: http://portal.suse.com/sdb/en/2003/10/pohletz_desktop_90.html What it indicates is that by default SuSE 9.0 plays...
8
by: vidya.bhagwath | last post by:
Hello Experts, I am using std::string object as a member variable in one of the my class. The same class member function operates on the std::string object and it appends some string to that...
0
by: Sabri.Pllana | last post by:
We apologize if you receive multiple copies of this call for papers. *********************************************************************** 2008 International Workshop on Multi-Core Computing...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.