It's been about a year since I've used C++, and I'm having a weird error that I can't remember why it does this, or if there's actually something wrong. My destructor gets called twice when I declare my object via the first method, and only once (correctly) when it declare it the 2nd time.

Wrong, destructor gets called twice.
Hash h;
h = new Hash(string);

Destructor gets called once.
Hash h = new Hash(string);


I remember about the copy constructor and assignment operator. And I have them both implemented I assume correctly. But I'm not sure what the deal is. I'm sure it's something stupid that writing in evil Java makes me forget.

Hash::Hash(const Hash& other){

  this->Map = other.Map;
  
}

Hash& Hash::operator=(const Hash& other){
  
  if (this != &other){
    this->Map=other.Map;
  }
  
  return *this;
}

Recommended Answers

All 8 Replies

Wrong, destructor gets called twice.
Hash h;
h = new Hash(string);

Destructor gets called once.
Hash h = new Hash(string);

Neither of these are right they attempt to assign an object of type Hash* to an object of type Hash I get the following 2 errors respectively compiling this code.

error: no match for ‘operator=’ in ‘h1 = (operator new(4u), (<statement>, ((Hash*)<anonymous>)))’

error: conversion from ‘Hash*’ to non-scalar type ‘Hash’ requested

Not only that but even if they did work there is no evidence that you have stored the pointers returned by new so that you can later delete them so you would have a memory leak (remember no garbage collection in C++).

Sorry I'm retarded. Forget the new in my code, that isn't in what I have in my IDE. I guess I should have copy and pasted instead of retyping it in here.

This is what doesn't ork
Hash h;
h = Hash(string);

This is what does
Hash h = Hash(string);

Sorry I'm retarded. Forget the new in my code, that isn't in what I have in my IDE. I guess I should have copy and pasted instead of retyping it in here.

This is what doesn't ork
Hash h;
h = Hash(string);

This is what does
Hash h = Hash(string);

Hash h;
h = Hash(string);

Hash(string) invokes default constructor and creates an object
this object is then copied to 'h' at h= Hash(string)

Hash h = Hash(string);
here you're creating an object and initialising it at the same time so just default constructor gets called.

Yeah I get that. But is there something that makes my object get created twice so the destructor gets called twice? I'm really confused on why it's calling the destructor twice. I can post more code if necessary

Hash h = Hash(string);

1 line of code, h gets created on the first line and initialised with string. 1 object, h, is constructed and destructed.

Hash h;
h = Hash(string);

2 lines of code (well statements they could be on the same line). Line 1 h is created using the default constructor. Line 2 an anonymous temporary object is created and initialised with string and then assigned to h using the assignment operator then the anonymous temporary object is destroyed. 2 objects are constructed, h and the anonymous temporary, and then destructed so there are 2 destructor calls.

It is the difference between initialisation and assignment.

Hash(string) invokes default constructor and creates an object
this object is then copied to 'h' at h= Hash(string)

I think you misunderstand me. The destructor of the same object is being called. How I know this is when my destructor gets called, I delete my pointers that h contains. But then it gets called again and it loops through the vector again (to delete teh pointers, that's where they are), but they're already gone so I get a seg fault from calling delete on random memory

Yeah I get that. But is there something that makes my object get created twice so the destructor gets called twice? I'm really confused on why it's calling the destructor twice. I can post more code if necessary

try delete() to get rid of the objects

Hash h = Hash(string);

1 line of code, h gets created on the first line and initialised with string. 1 object, h, is constructed and destructed.

Hash h;
h = Hash(string);

2 lines of code (well statements they could be on the same line). Line 1 h is created using the default constructor. Line 2 an anonymous temporary object is created and initialised with string and then assigned to h using the assignment operator then the anonymous temporary object is destroyed. 2 objects are constructed, h and the anonymous temporary, and then destructed so there are 2 destructor calls.

It is the difference between initialisation and assignment.

Hash(string) invokes default constructor and creates an object
this object is then copied to 'h' at h= Hash(string)

+1

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.