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

What does this mean(Copy Constructor type) !!

1
Hi,

I came across a statement as shown below:-
(CDerivedClass is derived from CBaseClass)
(pPtrToBaseClass is a pointer to CBaseClass)

CBaseClass *pBClass = NULL;

pBClass = new CDerivedClass(*(CDerivedClass *)pPtrToBaseClass);

Is this a Copy constructor and what does the above statement does....
Please explain in details in "Memory" terms.

Thanks,
Nayan
Feb 4 '08 #1
1 1364
weaknessforcats
9,208 Expert Mod 8TB
Firstly, this is crap code because of that cast.

But look at this line:
Expand|Select|Wrap|Line Numbers
  1. pBClass = new CDerivedClass(*(CDerivedClass *)pPtrToBaseClass);
  2.  
Take out the cast and you have:
Expand|Select|Wrap|Line Numbers
  1. pBClass = new CDerivedClass(*pPtrToBaseClass);
  2.  
Dereferencing a pointer to a base class object gives you the base class object itself. So, what they did was typecast the pointer to base class to a derived class pointer. By de-referencing that, you have a derived object. So the code becomes:
Expand|Select|Wrap|Line Numbers
  1. pBClass = new CDerivedClass(CDerivedClass);
  2.  
So it is, in fact a copy constructor call.

That stupid cast assumes the base class pointer is pointing a a CDerived object. If it's not, like there are several kinds of derived classes, then this cast will be to the incorrect type and your program will crash right here.

The code should be:
Expand|Select|Wrap|Line Numbers
  1. CDerivedClass pPtrToDerivedClass = new CDerivedClass
  2. pBClass = new CDerivedClass(*pPtrToDerivedClass);
  3.  
You cannot create a derived object by using a pointer to a base object. The base object doesn't know that there are derived objects.
Feb 4 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

354
by: Montrose... | last post by:
After working in c# for a year, the only conclusion I can come to is that I wish I knew c. All I need is Linux, the gnu c compiler and I can do anything. Web services are just open sockets...
5
by: Eric Lilja | last post by:
Using a macro, can I change what type an object is being cast to? I know, the initial respone to question might be an instinctive "ugly, don't even think about it!" or "don't use macros at all",...
15
by: damian birchler | last post by:
Hi I'm wondering of what type a structure is. Of course, it is a _structure_, but an array isn't an _array_ either. So of what type is a structure? I'd say a pointer, am I right?
0
by: trend | last post by:
Does WSE really care (when authenticating via PKI) what type of webserver it authenticates to? Example: I have 10client programs that I handed out to my friends.. with each of these client...
3
by: Steve Long | last post by:
Hello all. I'm having to do something I haven't done before and I don't know what type of control to use for this. This is basically like the "Title" box that you get in an MS PowerPoint slide when...
15
by: Leandro Ardissone | last post by:
Hi, I want to know what type is a variable. For example, I get the contents of an xml but some content is a list or a string, and I need to know what type it is. Thanks
5
by: garyusenet | last post by:
Console.WriteLine("Current OS: {0} ", Environment.OSVersion) I'm trying to find out what .OSVersion is. It returns an object which details the OS. I know it's not a method, because it doesnt...
31
by: Lane Straatman | last post by:
void s_sort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)) { size_t bytes; unsigned char *array, *after, *i, *j, *k, *p1, *p2, *end, swap; array = base;...
1
by: =?Utf-8?B?UGV0ZXI=?= | last post by:
I'm trying to understand namespaces and assemblies. How are they related and what does type mean in namespaces and assemblies?
1
by: mmf.stavelot | last post by:
How to find out what type of the data procedure in MSSQL 2005 returns? Using inquiry about sample. Example: SELECT paramtypes FROM sysprocedures; Result:
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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?

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.