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

copy constructor issues

momotaro
357 100+
is this code correct ?
Expand|Select|Wrap|Line Numbers
  1. DynamicVector::DynamicVector(const DynamicVector& copy)
  2. {
  3.     if(this != &copy)
  4.     {
  5.         this->length = copy.length;
  6.         for(int i = 0; i < copy.length; i++)
  7.             *(this->v+i) = *(copy.v+i);
  8.     }
  9. }
because when I do things like that:
Expand|Select|Wrap|Line Numbers
  1. DynamicVector v(5), t(7);
  2. v=t;
it works with or without the copy constructor !!!
and upon closing my console gives me that error in both cases:
Debug Assertion Failed!
Program:...o
...\MyProgram.exe
File:...\dbgdel.cpp
Line:52
Expression:_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
Apr 10 '11 #1
2 1456
Banfa
9,065 Expert Mod 8TB
Well you always have a copy constructor, if you don't write one the compiler generates a default on that does a plain copy of the variables. The problem with this is if you have data allocated to a pointer, both instances of the class end up pointing at the same piece of allocated data and which every gets destroyed first deletes that data leaving the other instance with an invalid pointer.

In your code you copy to this->v but a see no attempt to ensure that it points to a block of data that is long enough. If this->length != copy.length then you should be doing something about deleting the current this->v and allocating new data for it.
Apr 10 '11 #2
hype261
207 100+
Expand|Select|Wrap|Line Numbers
  1. DynamicVector v(5), t(7); 
  2. v=t;
  3.  
This code right here will call the assignment operator not the copy constructor.

To call the copy constructor use...

Expand|Select|Wrap|Line Numbers
  1. DynamicVector t(7);
  2. DynamicVector v = t;
  3.  
Apr 11 '11 #3

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

Similar topics

7
by: Dominique | last post by:
Hi Can anyone help here. I have defined a copy constructor: CString::CString (const CString &string) // copy constructor { Int16 size = string.GetLength() + 1; // this line generates error...
15
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including...
8
by: Jesper | last post by:
Hi, Does the concept "copy constructor" from c++ excist in c#. What is the syntax. best regards Jesper.
9
by: muzmail | last post by:
I have declared a copy constructor for a template class in a Visual C++ project but for some reason the compiler ignores it. I can put syntax errors in the copy constructor and the compiler ignores...
8
by: shuisheng | last post by:
Dear All, I am wondering how the default copy constructor of a derived class looks like. Does it look like class B : public A { B(const B& right) : A(right) {}
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, ...
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...
1
by: Scott Gifford | last post by:
Hello, I'm working on an providing an iterator interface to a database. The basic thing I'm trying to accomplish is to have my iterator read rows from the database and return constructed...
10
by: JosephLee | last post by:
In Inside C++ object Model, Lippman said there are four cases in which compile will sythesize a default constructor to initialize the member variables if the constructor is absent: 1. there is a...
4
by: Rahul | last post by:
Hi Everyone, It is well known that the input parameter which is passed to the copy constructor is passed as reference and not as as object. Because passing an object is as good as making another...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
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...
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...

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.