473,770 Members | 5,299 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

again the problem: the destructor is called twice

Hi all,

I posted my question two days ago, and tried to solve this problem.
but until now I didn't solve that. and I cut my codes so maybe this
time it is more readable.

/////////////////////////////////////////////////////
#ifndef MYCLASS_H_
#define MYCLASS_H_

#include <string>
#include <list>
//#include "name.h"

using namespace std;
class myclass
{
protected:
list<stringname list;
// map<int,vector< namenames;
public:

myclass();
myclass(const myclass &my);
~myclass();
// myclass& operator=(const myclass &it);
void AddName(const string &name);
void GetMyclass();
};

#endif

///////////////////////////////////////////////
myclass.cpp
#include "myclass.h"
#include <algorithm>
#include <iostream>

myclass::myclas s()
{

}

myclass::myclas s(const myclass &my)
{

namelist=my.nam elist;
}

myclass::~mycla ss()
{

namelist.erase( namelist.begin( ),namelist.end( ));
}
void myclass::AddNam e(const string &name)
{
list<string>::i terator ii;
ii=find(namelis t.begin(),namel ist.end(),name) ;

if(ii==namelist .end())
namelist.push_b ack(name);
}

void myclass::GetMyc lass()
{
list<string>::i terator ii;
for(ii=namelist .begin();ii!=na melist.end();ii ++)
cout<<*ii<<endl ;
}

/////////////////////////////////////////////////
test.h
#ifndef TEST_H_
#define TEST_H_

#include <string>
#include <map>
#include "myclass.h"

using namespace std;

class test
{
protected:
map<string,mycl ass*tests;

public:
test();
test(const test& mytest);
test& operator=(const test& ts);
~test();
void AddMyclass(cons t string &name,const myclass &my);
void GetTest();

};

#endif

////////////////////////////////////////////////////
test.cpp

#include "test.h"

#include <utility>
#include <iostream>

test::test()
{
}
test::test(cons t test& mytest)
{
tests=mytest.te sts;
}

test& test::operator =(const test &ts)
{
if(this!=&ts)
{
map<string,mycl ass*>::iterator ii;
for(ii=tests.be gin();ii!=tests .end();++ii)
delete(ii->second);
tests.clear();

tests=ts.tests;
}
return *this;
}

test::~test()
{
map<string,mycl ass*>::iterator ii;
for(ii=tests.be gin();ii!=tests .end();++ii)
delete(ii->second);
tests.clear();
}
void test::AddMyclas s(const string &name,const myclass &my)
{
map<string,mycl ass*>::iterator ii;
myclass* newmyclass=NULL ;

ii=tests.find(n ame);
if(ii==tests.en d())
{
newmyclass=new myclass(my);
tests[name]=newmyclass;
}
}

void test::GetTest()
{
map<string,mycl ass*>::iterator ii;
for(ii=tests.be gin();ii!=tests .end();ii++)
{
cout<<"the name is:"<<ii->first<<endl;
ii->second->GetMyclass() ;
}
}

////////////////////////////////////////////////////////////////////
call.h

#ifndef CALL_H_
#define CALL_H_

#include "test.h"

class call
{
public:
inline void SetTest(const test& ts){testagain=t s;}

protected:
test testagain;
};
#endif

///////////////////////////////////////////////////////////////////
main
#include "myclass.h"
#include "test.h"
#include "call.h"

int main()
{
test tt;
myclass my;
string ss[3]={"a","b","c" };
call ca;

for(int i=0;i<3;i++)
my.AddName(ss[i]);

my.GetMyclass() ;

tt.AddMyclass(" my class",my);

tt.GetTest();

ca.SetTest(tt);
return 0;
}

The problem is at the line ca.SetTest(tt). Could somebody tell me how
to solve it? Thanks.

Mar 23 '07 #1
9 3593
On Mar 23, 11:15 am, "David" <clam...@gmail. comwrote:
Hi all,

I posted my question two days ago, and tried to solve this problem.
but until now I didn't solve that. and I cut my codes so maybe this
time it is more readable.

/////////////////////////////////////////////////////
#ifndef MYCLASS_H_
#define MYCLASS_H_

#include <string>
#include <list>
//#include "name.h"

using namespace std;
class myclass
{
protected:
list<stringname list;
// map<int,vector< namenames;
public:

myclass();
myclass(const myclass &my);
~myclass();
// myclass& operator=(const myclass &it);
void AddName(const string &name);
void GetMyclass();

};

#endif

///////////////////////////////////////////////
myclass.cpp
#include "myclass.h"
#include <algorithm>
#include <iostream>

myclass::myclas s()
{

}

myclass::myclas s(const myclass &my)
{

namelist=my.nam elist;

}

myclass::~mycla ss()
{

namelist.erase( namelist.begin( ),namelist.end( ));

}

void myclass::AddNam e(const string &name)
{
list<string>::i terator ii;
ii=find(namelis t.begin(),namel ist.end(),name) ;

if(ii==namelist .end())
namelist.push_b ack(name);

}

void myclass::GetMyc lass()
{
list<string>::i terator ii;
for(ii=namelist .begin();ii!=na melist.end();ii ++)
cout<<*ii<<endl ;

}

/////////////////////////////////////////////////
test.h
#ifndef TEST_H_
#define TEST_H_

#include <string>
#include <map>
#include "myclass.h"

using namespace std;

class test
{
protected:
map<string,mycl ass*tests;

public:
test();
test(const test& mytest);
test& operator=(const test& ts);
~test();
void AddMyclass(cons t string &name,const myclass &my);
void GetTest();

};

#endif

////////////////////////////////////////////////////
test.cpp

#include "test.h"

#include <utility>
#include <iostream>

test::test()
{}

test::test(cons t test& mytest)
{
tests=mytest.te sts;

}

test& test::operator =(const test &ts)
{
if(this!=&ts)
{
map<string,mycl ass*>::iterator ii;
for(ii=tests.be gin();ii!=tests .end();++ii)
delete(ii->second);
tests.clear();

tests=ts.tests;
}
return *this;

}

test::~test()
{
map<string,mycl ass*>::iterator ii;
for(ii=tests.be gin();ii!=tests .end();++ii)
delete(ii->second);
tests.clear();}

void test::AddMyclas s(const string &name,const myclass &my)
{
map<string,mycl ass*>::iterator ii;
myclass* newmyclass=NULL ;

ii=tests.find(n ame);
if(ii==tests.en d())
{
newmyclass=new myclass(my);
tests[name]=newmyclass;
}

}

void test::GetTest()
{
map<string,mycl ass*>::iterator ii;
for(ii=tests.be gin();ii!=tests .end();ii++)
{
cout<<"the name is:"<<ii->first<<endl;
ii->second->GetMyclass() ;
}

}

////////////////////////////////////////////////////////////////////
call.h

#ifndef CALL_H_
#define CALL_H_

#include "test.h"

class call
{
public:
inline void SetTest(const test& ts){testagain=t s;}

Here you make a copy of test. There are now two objects in existence,
so two destructors will be called at some point.

>
protected:
test testagain;};

#endif

///////////////////////////////////////////////////////////////////
main
#include "myclass.h"
#include "test.h"
#include "call.h"

int main()
{
test tt;
myclass my;
string ss[3]={"a","b","c" };
call ca;

for(int i=0;i<3;i++)
my.AddName(ss[i]);

my.GetMyclass() ;

tt.AddMyclass(" my class",my);

tt.GetTest();

ca.SetTest(tt);
return 0;

}

The problem is at the line ca.SetTest(tt). Could somebody tell me how
to solve it? Thanks.
You could pass ownership of the test object rather than making a copy.
One way to do that is to use std::auto_ptr, which signifies unique
ownership. So if your "call" class accepts an auto_ptr<test>, the
implicit understanding is that it takes sole ownership of that object.

Cheers! --M

Mar 23 '07 #2
"David" <cl*****@gmail. comwrote in message
news:11******** **************@ b75g2000hsg.goo glegroups.com.. .
Hi all,

I posted my question two days ago, and tried to solve this problem.
but until now I didn't solve that. and I cut my codes so maybe this
time it is more readable.

/////////////////////////////////////////////////////
#ifndef MYCLASS_H_
#define MYCLASS_H_

#include <string>
#include <list>
//#include "name.h"

using namespace std;
class myclass
{
protected:
list<stringname list;
// map<int,vector< namenames;
public:

myclass();
myclass(const myclass &my);
~myclass();
// myclass& operator=(const myclass &it);
void AddName(const string &name);
void GetMyclass();
};

#endif

///////////////////////////////////////////////
myclass.cpp
#include "myclass.h"
#include <algorithm>
#include <iostream>

myclass::myclas s()
{

}

myclass::myclas s(const myclass &my)
{

namelist=my.nam elist;
}

myclass::~mycla ss()
{

namelist.erase( namelist.begin( ),namelist.end( ));
}
void myclass::AddNam e(const string &name)
{
list<string>::i terator ii;
ii=find(namelis t.begin(),namel ist.end(),name) ;

if(ii==namelist .end())
namelist.push_b ack(name);
}

void myclass::GetMyc lass()
{
list<string>::i terator ii;
for(ii=namelist .begin();ii!=na melist.end();ii ++)
cout<<*ii<<endl ;
}

/////////////////////////////////////////////////
test.h
#ifndef TEST_H_
#define TEST_H_

#include <string>
#include <map>
#include "myclass.h"

using namespace std;

class test
{
protected:
map<string,mycl ass*tests;

public:
test();
test(const test& mytest);
test& operator=(const test& ts);
~test();
void AddMyclass(cons t string &name,const myclass &my);
void GetTest();

};

#endif

////////////////////////////////////////////////////
test.cpp

#include "test.h"

#include <utility>
#include <iostream>

test::test()
{
}
test::test(cons t test& mytest)
{
tests=mytest.te sts;
}
Above is your copy constructor. Yet you have a map of pointers you are not
copying correctly. Remember, your test destructor will delete the pointers,
and generally I've found that when a copy constructor is called there is a
temporary made somewhere that needs to be deleted. Pointers moving from
class to class is a bad idea where there is a delete. Generally what you
have to do is duplicate the objects the pointers are pointing to. new a new
pointer, assign the value so that when the old one gets deleted you don't
lose the data.

If I have a class that has pointers to newed objects, I always make the copy
and assignment operators private so they can't be called. Unless you work
with smart pointers you'll have issues.

Think about your line later:
ca.SetTest(tt);
ca is to have a map of the ponters, but so is tt. Yet you only newed them
once. You have 2 different objects (attempting to) point to the same
memory, but they get deleted. If you have 2 objects with pointers, for an
assignment or copy you have to copy the objects the memory is pointing to so
you have 2 seperate memories they are pointing to and their deletes work
correctly, or you have to think very very carefully about object ownership.
After cs.SetTest(tt) who do you expect to "own" the memory the pointers are
pointing to? You can't have 2 seperate instances owning the same pointers.
Unless you use smart pointers of some type.

You have a design flaw here and you are going to have to rethink your
classes.
test& test::operator =(const test &ts)
{
if(this!=&ts)
{
map<string,mycl ass*>::iterator ii;
for(ii=tests.be gin();ii!=tests .end();++ii)
delete(ii->second);
tests.clear();

tests=ts.tests;
}
return *this;
}

test::~test()
{
map<string,mycl ass*>::iterator ii;
for(ii=tests.be gin();ii!=tests .end();++ii)
delete(ii->second);
tests.clear();
}
void test::AddMyclas s(const string &name,const myclass &my)
{
map<string,mycl ass*>::iterator ii;
myclass* newmyclass=NULL ;

ii=tests.find(n ame);
if(ii==tests.en d())
{
newmyclass=new myclass(my);
tests[name]=newmyclass;
}
}

void test::GetTest()
{
map<string,mycl ass*>::iterator ii;
for(ii=tests.be gin();ii!=tests .end();ii++)
{
cout<<"the name is:"<<ii->first<<endl;
ii->second->GetMyclass() ;
}
}

////////////////////////////////////////////////////////////////////
call.h

#ifndef CALL_H_
#define CALL_H_

#include "test.h"

class call
{
public:
inline void SetTest(const test& ts){testagain=t s;}

protected:
test testagain;
};
#endif

///////////////////////////////////////////////////////////////////
main
#include "myclass.h"
#include "test.h"
#include "call.h"

int main()
{
test tt;
myclass my;
string ss[3]={"a","b","c" };
call ca;

for(int i=0;i<3;i++)
my.AddName(ss[i]);

my.GetMyclass() ;

tt.AddMyclass(" my class",my);

tt.GetTest();

ca.SetTest(tt);
return 0;
}

The problem is at the line ca.SetTest(tt). Could somebody tell me how
to solve it? Thanks.

Mar 23 '07 #3
On 23 Mar 2007 08:23:01 -0700, "mlimber" wrote:
>On Mar 23, 11:15 am, "David" <clam...@gmail. comwrote:
>>
int main()
{
test tt;
myclass my;
string ss[3]={"a","b","c" };
call ca;

for(int i=0;i<3;i++)
my.AddName(ss[i]);

my.GetMyclass() ;

tt.AddMyclass(" my class",my);

tt.GetTest();

ca.SetTest(tt);
return 0;

}

The problem is at the line ca.SetTest(tt). Could somebody tell me how
to solve it? Thanks.

You could pass ownership of the test object rather than making a copy.
One way to do that is to use std::auto_ptr, which signifies unique
ownership. So if your "call" class accepts an auto_ptr<test>, the
implicit understanding is that it takes sole ownership of that object.
Goodness gracious! tt is a local automatic object. No need to "pass
ownership" or use a 'smart pointer'. Better make the copy constructor
and operator= for all classes private (leave them un-implemented) and
your code will work with some changes.

Best regards,
Roland Pibinger
Mar 23 '07 #4
David wrote:
Hi all,

I posted my question two days ago, and tried to solve this problem.
but until now I didn't solve that. and I cut my codes so maybe this
time it is more readable.

/////////////////////////////////////////////////////
#ifndef MYCLASS_H_
#define MYCLASS_H_

#include <string>
#include <list>
//#include "name.h"

using namespace std;
class myclass
{
protected:
list<stringname list;
// map<int,vector< namenames;
public:

myclass();
myclass(const myclass &my);
~myclass();
// myclass& operator=(const myclass &it);
void AddName(const string &name);
void GetMyclass();
};

#endif

///////////////////////////////////////////////
myclass.cpp
#include "myclass.h"
#include <algorithm>
#include <iostream>

myclass::myclas s()
{

}

myclass::myclas s(const myclass &my)
{

namelist=my.nam elist;
}

myclass::~mycla ss()
{

namelist.erase( namelist.begin( ),namelist.end( ));
}
void myclass::AddNam e(const string &name)
{
list<string>::i terator ii;
ii=find(namelis t.begin(),namel ist.end(),name) ;

if(ii==namelist .end())
namelist.push_b ack(name);
}

void myclass::GetMyc lass()
{
list<string>::i terator ii;
for(ii=namelist .begin();ii!=na melist.end();ii ++)
cout<<*ii<<endl ;
}

/////////////////////////////////////////////////
test.h
#ifndef TEST_H_
#define TEST_H_

#include <string>
#include <map>
#include "myclass.h"

using namespace std;

class test
{
protected:
map<string,mycl ass*tests;

public:
test();
test(const test& mytest);
test& operator=(const test& ts);
~test();
void AddMyclass(cons t string &name,const myclass &my);
void GetTest();

};

#endif

////////////////////////////////////////////////////
test.cpp

#include "test.h"

#include <utility>
#include <iostream>

test::test()
{
}
test::test(cons t test& mytest)
{
tests=mytest.te sts;
}

test& test::operator =(const test &ts)
{
if(this!=&ts)
{
map<string,mycl ass*>::iterator ii;
for(ii=tests.be gin();ii!=tests .end();++ii)
delete(ii->second);
tests.clear();

tests=ts.tests;
}
return *this;
}

test::~test()
{
map<string,mycl ass*>::iterator ii;
for(ii=tests.be gin();ii!=tests .end();++ii)
delete(ii->second);
tests.clear();
}
void test::AddMyclas s(const string &name,const myclass &my)
{
map<string,mycl ass*>::iterator ii;
myclass* newmyclass=NULL ;

ii=tests.find(n ame);
if(ii==tests.en d())
{
newmyclass=new myclass(my);
tests[name]=newmyclass;
}
}

void test::GetTest()
{
map<string,mycl ass*>::iterator ii;
for(ii=tests.be gin();ii!=tests .end();ii++)
{
cout<<"the name is:"<<ii->first<<endl;
ii->second->GetMyclass() ;
}
}

////////////////////////////////////////////////////////////////////
call.h

#ifndef CALL_H_
#define CALL_H_

#include "test.h"

class call
{
public:
inline void SetTest(const test& ts){testagain=t s;}

protected:
test testagain;
};
#endif

///////////////////////////////////////////////////////////////////
main
#include "myclass.h"
#include "test.h"
#include "call.h"

int main()
{
test tt;
myclass my;
string ss[3]={"a","b","c" };
call ca;

for(int i=0;i<3;i++)
my.AddName(ss[i]);

my.GetMyclass() ;

tt.AddMyclass(" my class",my);

tt.GetTest();

ca.SetTest(tt);
return 0;
}

The problem is at the line ca.SetTest(tt). Could somebody tell me how
to solve it? Thanks.
Your copy constructor hints a shallow copy while your copy assignment
operator hints a deep copy. You are contradicting youself in terms of
exactly what you want to do with the private data in test class.

Fei
Mar 23 '07 #5
On Mar 23, 11:57 am, rpbg...@yahoo.c om (Roland Pibinger) wrote:
On 23 Mar 2007 08:23:01 -0700, "mlimber" wrote:
You could pass ownership of the test object rather than making a copy.
One way to do that is to use std::auto_ptr, which signifies unique
ownership. So if your "call" class accepts an auto_ptr<test>, the
implicit understanding is that it takes sole ownership of that object.

Goodness gracious! tt is a local automatic object. No need to "pass
ownership" or use a 'smart pointer'. Better make the copy constructor
and operator= for all classes private (leave them un-implemented) and
your code will work with some changes.
Like I said, there are multiple ways to accomplish this, and passing
ownership like I described is one common and useful way to do it.
Compare this FAQ by the Creator on using auto_ptr to pass ownership:

http://www.research.att.com/~bs/bs_f...l#memory-leaks

Of course, the "Big Three" (or probably better, the "Big Two" --
http://www.artima.com/cppsource/bigtwo.html) problems ought to be
fixed in any case.

Cheers! --M

Mar 23 '07 #6
On Mar 23, 3:10 pm, "mlimber" <mlim...@gmail. comwrote:
On Mar 23, 11:57 am, rpbg...@yahoo.c om (Roland Pibinger) wrote:
On 23 Mar 2007 08:23:01 -0700, "mlimber" wrote:
>You could pass ownership of the test object rather than making a copy.
>One way to do that is to use std::auto_ptr, which signifies unique
>ownership. So if your "call" class accepts an auto_ptr<test>, the
>implicit understanding is that it takes sole ownership of that object.
Goodness gracious! tt is a local automatic object. No need to "pass
ownership" or use a 'smart pointer'. Better make the copy constructor
and operator= for all classes private (leave them un-implemented) and
your code will work with some changes.

Like I said, there are multiple ways to accomplish this, and passing
ownership like I described is one common and useful way to do it.
Compare this FAQ by the Creator on using auto_ptr to pass ownership:

http://www.research.att.com/~bs/bs_f...l#memory-leaks

Of course, the "Big Three" (or probably better, the "Big Two" --http://www.artima.com/cppsource/bigtwo.html) problems ought to be
fixed in any case.

Cheers! --M
Thanks all for your help. I think I need a "deep copy" in my
copyconstructor . sorry I am new in C++, so when I tried to do below,
there was something wrong with this:
test::test(cons t test& mytest)
{

map<string,mycl ass*>::iterator ii;

for(ii=mytest.t ests.begin();ii !=mytest.tests. end();++ii)
{
myclass* my=new myclass;
memcpy(my,ii->second,sizeof( ii->second));
tests.insert(ma ke_pair(ii->first,my));
}
}

there was an error: "c:\Ug\Solution 1\test\test.cpp (16): error C2679:
binary '=' : no operator found which takes a right-hand operand of
type 'std::_Tree<_Tr aits>::const_it erator' " at the for loop point.
can anybody tell me why and what's the right way to do the copy
constructor? thanks

Mar 23 '07 #7
"David" <cl*****@gmail. comwrote in message
news:11******** **************@ l75g2000hse.goo glegroups.com.. .
On Mar 23, 3:10 pm, "mlimber" <mlim...@gmail. comwrote:
>On Mar 23, 11:57 am, rpbg...@yahoo.c om (Roland Pibinger) wrote:
On 23 Mar 2007 08:23:01 -0700, "mlimber" wrote:
You could pass ownership of the test object rather than making a copy.
One way to do that is to use std::auto_ptr, which signifies unique
ownership. So if your "call" class accepts an auto_ptr<test>, the
implicit understanding is that it takes sole ownership of that object.
Goodness gracious! tt is a local automatic object. No need to "pass
ownership" or use a 'smart pointer'. Better make the copy constructor
and operator= for all classes private (leave them un-implemented) and
your code will work with some changes.

Like I said, there are multiple ways to accomplish this, and passing
ownership like I described is one common and useful way to do it.
Compare this FAQ by the Creator on using auto_ptr to pass ownership:

http://www.research.att.com/~bs/bs_f...l#memory-leaks

Of course, the "Big Three" (or probably better, the "Big
Two" --http://www.artima.com/cppsource/bigtwo.html) problems ought to be
fixed in any case.

Cheers! --M

Thanks all for your help. I think I need a "deep copy" in my
copyconstructor . sorry I am new in C++, so when I tried to do below,
there was something wrong with this:
test::test(cons t test& mytest)
{

map<string,mycl ass*>::iterator ii;

for(ii=mytest.t ests.begin();ii !=mytest.tests. end();++ii)
{
myclass* my=new myclass;
memcpy(my,ii->second,sizeof( ii->second));
tests.insert(ma ke_pair(ii->first,my));
}
}

there was an error: "c:\Ug\Solution 1\test\test.cpp (16): error C2679:
binary '=' : no operator found which takes a right-hand operand of
type 'std::_Tree<_Tr aits>::const_it erator' " at the for loop point.
can anybody tell me why and what's the right way to do the copy
constructor? thanks
The parameter to the copy constructor is a const test& mytest. mytest is
constant. You are trying to use a non-constant iterator on it, which isn't
allowed.
Try:

map<string,mycl ass*>::const_it erator ii;
Mar 23 '07 #8
On Mar 23, 3:26 pm, "David" <clam...@gmail. comwrote:
On Mar 23, 3:10 pm, "mlimber" <mlim...@gmail. comwrote:
Of course, the "Big Three" (or probably better, the "Big Two" --http://www.artima.com/cppsource/bigtwo.html) problems ought to be
fixed in any case.

Thanks all for your help. I think I need a "deep copy" in my
copyconstructor . sorry I am new in C++, so when I tried to do below,
there was something wrong with this:
test::test(cons t test& mytest)
{

map<string,mycl ass*>::iterator ii;
You need

map<string,mycl ass*>::const_it erator ii;

since mytest is const.
>
for(ii=mytest.t ests.begin();ii !=mytest.tests. end();++ii)
{
myclass* my=new myclass;
memcpy(my,ii->second,sizeof( ii->second));
tests.insert(ma ke_pair(ii->first,my));
}

}

there was an error: "c:\Ug\Solution 1\test\test.cpp (16): error C2679:
binary '=' : no operator found which takes a right-hand operand of
type 'std::_Tree<_Tr aits>::const_it erator' " at the for loop point.
can anybody tell me why and what's the right way to do the copy
constructor? thanks
Read the article on the law of the Big Two that I gave in my previous
post.

Here's a tip: If you see memcpy in C++ code, that's bad (usually).
Prefer to give myclass proper copy semantics so you can say:

*my = *( ii->second );

Better still would be to use a smart pointer that does -- depending on
your needs -- either reference counting (e.g., std::tr1::share d_ptr,
boost::shared_p tr, Loki::SmartPtr, or the one in FAQ 16.22 and
following) or deep copying (e.g., Loki::SmartPtr) for you
automatically.

Cheers! --M

Mar 23 '07 #9
On 23 Mar 2007 12:26:52 -0700, "David" <cl*****@gmail. comwrote:
>I think I need a "deep copy" in my copyconstructor .
You need no copy constructor at all. You can and, IMO, should
implement your library with disabled (= private) copy constructors and
assignmet operators. There is no need to duplicate and dynamically
allocate objects.

Best wishes,
Roland Pibinger
Mar 23 '07 #10

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

Similar topics

6
15732
by: Alexander Stippler | last post by:
Hi, I have some question about the usage of delete. I have some reference counted classes, all derived from SharedObject. It's destructor looks like this: ~SharedObject() { if (--references_ == 0) { delete this;
52
27040
by: Newsnet Customer | last post by:
Hi, Statement 1: "A dynamically created local object will call it's destructor method when it goes out of scope when a procedure returms" Agree. Statement 2: "A dynamically created object will call it's destructor when it is made a target of a delete".
9
8277
by: sahukar praveen | last post by:
Hello, This is the program that I am trying. The program executes but does not give me a desired output. ********************************************** #include <iostream.h> #include <iomanip.h> #include <string.h>
20
2753
by: frs | last post by:
For memory economization, I need to get rid if the virtual destructor. Under this constraint I get caught up in a design, where I need to call a destructor of a derived class from a base class. Briefly it could be displayed like the code below. It ends up deleting the member 'x' of 'B' twice. Why? Is there a way around? Thanks Frank
14
3209
by: gurry | last post by:
Suppose there's a class A. There's another class called B which looks like this: class B { private: A a; public : B() { a.~A() } }
2
1937
by: Kuku | last post by:
#include<iostream> using namespace std; class Mammal { private: int age, weight;
11
1993
by: AB | last post by:
Hi All, I've got an array of objects, during the execution of the program I'd like to assign a particular object to a certain element in the object array. The sample code's like this... class ClassA { public: ClassA()
4
2017
by: David | last post by:
Hi all, something wrong with my code, it seems that the destructor function has been called twice. I don't know why.here is my codes Fixture.h #ifndef _FIXTURE_H #define _FIXTURE_H #include <string> #include <map> #include <list>
12
2359
by: mc | last post by:
Hi Experts, This may be obvious but I can't find anything one way or another. We have a class which is always allocated using new, e.g. Foo* foo = new Foo() so we came up with the idea of releasing the memory from the destructor as follows: Foo::Foo() { // Initialize stuff
0
9591
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10225
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10053
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10001
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9867
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7415
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3573
muto222
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.