Board index » cppbuilder » Returning Class Objects from a Function

Returning Class Objects from a Function


2008-02-23 08:37:09 AM
cppbuilder95
Lets say I have a new class called myclass.
I want to do this.....
myclass FooBar(int x)
{
myclass a;
a.get(x); // some function called get
return a;
}
then
myclass w = FooBar(5);
Just like AnsiStrings or WideStrings
any idea.....
Thanks
Colin B Maharaj
(PS: was not sure if to put this in VCL writing or C++)
 
 

Re:Returning Class Objects from a Function

"Colin B Maharaj" < XXXX@XXXXX.COM >wrote in message
Quote
I want to do this.....
In order to do that, your class needs to have a copy constructor and an
assignment operator implemented, ie:
class myclass
{
public:
myclass();
myclass(const myclass &src);
myclass& operator=(const myclass &rhs);
};
myclass::myclass()
{
// initialize data members to default values...
}
myclass::myclass(const myclass &src)
{
// copy data members from src...
}
myclass& myclass::operator=(const myclass &rhs)
{
// copy data members from rhs...
return *this;
}
Gambit
 

Re:Returning Class Objects from a Function

"Colin B Maharaj" < XXXX@XXXXX.COM >wrote in message
Quote
Just like AnsiStrings or WideStrings
To add to what Remy wrote you should decide if reference counting is
desirable for your class.
AnsiString attempts to use one copy of a string's data as long as it can.
The copy construction and assignment operation are simple reference count
bumps. If code attempts to modify its string's data then it copies its
contents at that time and releases its reference to the original string's
data.
However, some class designs might want to allow each copy of a reference
counted object to modify the original object's data. The design is up to
you and depends on the purpose of the class.
Quote
(PS: was not sure if to put this in VCL writing or C++)
This would have been more appropriate in language.cpp.
Clayton
 

{smallsort}

Re:Returning Class Objects from a Function

Thanks a lot Remy, you are a real
champ!! This is the one I missed out.
Quote
>>myclass(const myclass &src);
Frankly I should be doing
some more reading on this and other
issues.
Remy Lebeau (TeamB) wrote:
Quote
"Colin B Maharaj" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...

>I want to do this.....

In order to do that, your class needs to have a copy constructor and an
assignment operator implemented, ie:

class myclass
{
public:
myclass();
myclass(const myclass &src);
myclass& operator=(const myclass &rhs);
};

myclass::myclass()
{
// initialize data members to default values...
}

myclass::myclass(const myclass &src)
{
// copy data members from src...
}

myclass& myclass::operator=(const myclass &rhs)
{
// copy data members from rhs...
return *this;
}


Gambit


 

Re:Returning Class Objects from a Function

I was explaining to Remy that I should be doing some
more reading. Is this behavior that I wanted a C++
specification? Can I see it somewhere? Do you have
a source.
Thanks again for your help
:)
Clayton Arends wrote:
Quote
"Colin B Maharaj" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...

>Just like AnsiStrings or WideStrings

To add to what Remy wrote you should decide if reference counting is
desirable for your class.

AnsiString attempts to use one copy of a string's data as long as it
can. The copy construction and assignment operation are simple reference
count bumps. If code attempts to modify its string's data then it
copies its contents at that time and releases its reference to the
original string's data.

However, some class designs might want to allow each copy of a reference
counted object to modify the original object's data. The design is up
to you and depends on the purpose of the class.

>(PS: was not sure if to put this in VCL writing or C++)

This would have been more appropriate in language.cpp.

Clayton
 

Re:Returning Class Objects from a Function

"Colin B Maharaj" < XXXX@XXXXX.COM >wrote in message
Quote
Is this behavior that I wanted a C++
specification? Can I see it somewhere? Do you have
a source.
I don't know if it's a specification. Here is a simple example though:
class SomeClass
{
private:
struct InternalData
{
unsigned RefCount;
// all data elements
};
InternalData* mData;
public:
SomeClass();
SomeClass(SomeClass const& rhs);
~SomeClass();
SomeClass& operator=(SomeClass const& rhs);
};
SomeClass::SomeClass()
{
mData = new InternalData;
mData->RefCount = 1;
}
SomeClass::SomeClass(SomeClass const& rhs)
{
mData = rhs.mData;
++mData->RefCount;
}
SomeClass::~SomeClass()
{
if (--mData->RefCount == 0)
delete mData;
}
SomeClass& SomeClass::operator=(SomeClass const& rhs)
{
++rhs.mData->RefCount;
--mData->RefCount;
mData = rhs.mData;
}
The InternalData class could just as easily be made a hidden class exposed
only to the cpp file that houses SomeClass but I've shown it inline to make
the example easier to read.
This example contains all of the basics of a reference-counted class. It
manages an internal pointer where the actual data resides. That internal
pointer maintains a reference count so that all objects know how many other
objects refer to that data.
All access to the internal data is achieved through the use of getter/setter
methods (in C++Builder you can also use properties to make the notation
friendlier). If the modifying of an object should create a new copy of the
object (like AnsiString does) then you would do something like this:
// Add these to the class declaration
private:
int get_SomeMember();
void set_SomeMember(int Value);
public:
__property int SomeMember =
{ read = get_SomeMember, write = set_SomeMember };
//
void SomeClass::set_SomeMember(int Value)
{
if (mData->RefCount>1)
{
InternalData* newData = new InternalData;
// Copy all data from mData to newData. If copying involves
// allocating resources then use RAII techniques to protect
// newData and the other resources until it can be assigned below
newData->RefCount = 1;
--mData->RefCount; // Release the original data
mData = newData; // Hook into the new data
}
mData->SomeMember = Value;
}
HTH,
Clayton