🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.migration
2 messages
2 total messages Started by Cherubino Ferrario Sun, 16 Jul 2006 17:48
Problem when i return a Class
#173
Author: Cherubino Ferrario
Date: Sun, 16 Jul 2006 17:48
34 lines
1686 bytes
Hello, i have a Big problem with C++ Builder 6 enterprise:

i write this simple code

main()

 {
   class1 Temp;
   class2 Temp1;

    Temp=Temp1.CompileClass();

 }

class1  __fastcall Calss2::CompileClass(void)
 {
  class1  Istance;

   return Istance;
 }

When the program go to line return Istance he call the destructor of class1 
and than the operator = of class1. I have acces viotation because Istance is 
not allocated.

If i compile the same program with c++ builder 5 professional the program 
work well.

Can you help me?  I must set some parameters in the Builder 6 project 
option?

Thanks for your attention

Cherubino Ferrario 
Re: Problem when i return a Class
#174
Author: Remy Lebeau (TeamB)
Date: Mon, 17 Jul 2006 03:44
23 lines
1890 bytes
"Cherubino Ferrario" <cherferr@tin.it> wrote in message
news:44ba7c21@newsgroups.borland.com...

> When the program go to line return Istance he call the
> destructor of class1 and than the operator = of class1.

As it should be, because that is what is really happening.  You have
declared a local instance of class1 inside of CompileClass() that will be
destroyed when it goes out of scope.  When you call the 'return' statement,
the '=' operator is used to assign that local instance to the instance that
is used for the return value, before the local instance is then destroyed.

> I have acces viotation because Istance is not allocated.

Yes, it is.  Your AV has to be caused by something else.  What does class1
actually look like?  My guess is that your class contains data that you are
not duplicating inside the '=' operator properly, so the data ends up being
deleted/released multiple times incorrectly.  Please show a more complete
code snippet.


Gambit
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads