473,396 Members | 1,678 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.

Calling copy constructor on void *

I have a void * variable1, which is really a pointer to a class. I want
to copy the class itself (not just the pointer) onto another void *
variable2, using the variable1's class's copy constructor (I need to do
more than just a shallow copy). Is there any way to do this? Thanks in
advance.

Steve

Jul 19 '05 #1
2 3027
"Ramiraf" <bl**@blah.com> wrote...
I have a void * variable1, which is really a pointer to a class. I want
to copy the class itself (not just the pointer) onto another void *
variable2, using the variable1's class's copy constructor (I need to do
more than just a shallow copy). Is there any way to do this?


You would need to [statically] cast the pointer to the class
pointer and then do 'new'. Something like

void *variable1;
... // somehow 'variable1' now points to an instance
// of MyClass
void *variable2 =
new MyClass(*static_cast<MyClass*>(variable1));

(of course 'MyClass' needs to be defined here).

Victor
Jul 19 '05 #2
On Mon, 14 Jul 2003 10:12:16 -0700, Ramiraf <bl**@blah.com> wrote:
I have a void * variable1, which is really a pointer to a class. I want
to copy the class itself (not just the pointer) onto another void *
variable2, using the variable1's class's copy constructor (I need to do
more than just a shallow copy). Is there any way to do this? Thanks in
advance.

Steve

The key in doing a deep copy is performing a cast on your pointer to
get to the goodies in the class itself. You can use an old C-style
cast, or one of the new C++ casts, dynamic_cast, static_cast, etc.

Once youv'e casted to your class, you can call an operator= you have
implemented, or some funky deep-copy global method youv'e written;
whatever flavor poison you choose.

A bigger question is, why have you casted down your class pointer to
void*? There are very often good, valid reasons to do so. But using
void* as a generic pointer is usually (IMO) an over-used, cliche hack
around bad design.

</dib>

John Dibling
email: dib@substitute_my_full_last_name_here.com
Witty banter omitted for your protection
Jul 19 '05 #3

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

Similar topics

4
by: franky.backeljauw | last post by:
Hello, I have a problem with using a copy constructor to convert an object of a templated class to object of another templated class. Let me first include the code (my question is below): ...
42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
5
by: I wish | last post by:
I wrote some program #include <iostream> class A { public: A() {} A( const A& t ) { std::cout << "Good" << std::endl; } }; A f()
11
by: Kurt Krueckeberg | last post by:
Given a class X class X { public: X(int); X(const X&); //. . . }; Is this line X x1(1); the same thing as X x2 = 2;
7
by: Kelly Mandrake | last post by:
I've implemented a class with operator+ overloaded and I also provided my own copy constructor; The problem is my copy constructor is being called twise and I dont understand why. I believe the...
11
by: csudha | last post by:
Hi all, Can you please explain me the usage of copy constructor and how do we use it. Give me some example. Regards, Sudha.
4
by: Michael | last post by:
Hello, I want to use an object (LowCut) within another object (SampleRateConverter) like it is written as follows: class SampleRateConverter { public: SampleRateConverter( int...
8
by: rKrishna | last post by:
I was trying to understand the real need for copy constructors. From literature, the main reason for redfinition of copy constructor in a program is to allow deep copying; meaning ability to make...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
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: 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
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,...
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
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
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.