473,473 Members | 1,947 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to make a copy of a Class that is passed by a pointer.

1 New Member
I have a function that is passing a class by pointer.

void showPassengers(CQueue*);

in my function I have it called q. I need to make a copy of it so I can manipulate the data and not mess with the original.

MAIN

CQueue qPassengers[LINES];

showPassengers(qPassengers);


IN THE FUNCTION

i was trying to do something like this. But it doesnt work.
Any suggestions. Thanks

void showPassengers(CQueue *q)
{


CQueue *copy_q = new CQueue (*q);

memcpy(copy_q, q);
Apr 20 '15 #1
1 1386
weaknessforcats
9,208 Recognized Expert Moderator Expert
You make a copy of a C++ object this way:

Expand|Select|Wrap|Line Numbers
  1. CQueue obj;
  2.  
  3. CQueue thecopy(obj);
This is a call to the class copy constructor.

In your code this looks like exactly what you have written...without the memcpy, of course.

All those C mem... functions need to be avoided since they have no idea how the memory is structured.
Apr 20 '15 #2

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

Similar topics

8
by: Nick | last post by:
I have the following code: var obj = {a:0, b:1, ...} function f() {...} obj.f = f // This will make a instance method? How to make a Class method for the class of obj? Or what's the...
6
by: Ash Lux | last post by:
Does the C++ vector template class use pointer-based link lists? Could I get a link explaining how it works, regardless of what it uses? Thank You, Ash Lux
6
by: Julie | last post by:
What would be the primary motivation to make a class 'sealed' (meaning that you can't derive from it) in C++? (I understand that there is currently no sealed keyword in C++, but that there are...
6
by: marco_segurini | last post by:
Hi, the following sample code shows a compiler error I get trying to build some old code with the last CL compiler (vers 13.10.3077): //----- begin #include <iostream> namespace ns {
1
by: ATS | last post by:
HOWTO Make StreamReader class not block. Please help, I'm trying to use the Std-Out from a process I launch, to read back data from the process, until either the data I expcet is passed, or a...
8
by: gk245 | last post by:
This is part of a bigger program, but i made it simple (basically the OT_hours function is supposed to determine if the number entered is over 40 hours or not and return the appropriate answer): ...
7
by: Mohan | last post by:
Hi, What are the advantages/disadvantages of using a pointer instead of Reference in the Copy Constructor ? For Example, Writing the Copy constructor for the Class "Temp" as below, ...
5
by: Grant Merwitz | last post by:
I have a basic object in an array. I bind this class to a gridview. How can i make this class sortable? Here's an example class public class MyClass { public MyClass(classname, classtype)
5
by: Gaijinco | last post by:
I know that by default every class has overloaded operator= and the copy constructor. How then can I make a class uncloneable? Thanks.
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
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...
1
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...
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...
0
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 ...
0
muto222
php
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.