Discussion:
Return a reference to array
(too old to reply)
MFCQuery
2006-05-22 13:39:27 UTC
Permalink
Hi,
I am facing a problem in returning a array type variable from a function.
Suppose i have following structure.

struct MyArray:public CArray<CString,CString>
{
};

I have a function say function1 from which i want to return MyArray.So the
function declaration is like
MyArray function1()
{
MyArray Object;
//I add some strings to Object and return it
return Object;
}

And in my main program i call function 1 as
MyArray &Obj=function1();
After executing this statement i see that Obj doesn't contain those strings
which i add in function1. The size of Obj is zero.

Can someone tell me how can i return a CArray type from a function?

Thanks
David Wilkinson
2006-05-22 16:57:42 UTC
Permalink
Post by MFCQuery
Hi,
I am facing a problem in returning a array type variable from a function.
Suppose i have following structure.
struct MyArray:public CArray<CString,CString>
{
};
I have a function say function1 from which i want to return MyArray.So the
function declaration is like
MyArray function1()
{
MyArray Object;
//I add some strings to Object and return it
return Object;
}
And in my main program i call function 1 as
MyArray &Obj=function1();
After executing this statement i see that Obj doesn't contain those strings
which i add in function1. The size of Obj is zero.
Can someone tell me how can i return a CArray type from a function?
Thanks
MFCQuery:

You are not returning a reference here; you are returning a value which
is a copy of Object. But this returned object is temporary, so you
should not be making a reference from it. Does it work if you copy the
returned object:

MyArray Obj = function1();

? Personally, I never use these MFC collection classes (STL is so much
better), but I am surprised your code even compiles because I thought
CArray did not have a copy constructor. Also, I believe it is more
efficient to use CArray<CString, CString&>.

IMO, a better design would be

class MyArray:public CArray<CString,CString&>
{
};

void function1(MyArray& obj)
{
//I add some strings to obj
}

MyArray obj;
function1(obj);

This avoids all copying of MyArray objects.

David Wilkinson
MFCQuery
2006-05-23 04:26:11 UTC
Permalink
Post by David Wilkinson
You are not returning a reference here; you are returning a value which
is a copy of Object. But this returned object is temporary, so you
should not be making a reference from it. Does it work if you copy the
MyArray Obj = function1();
This statement is not valid. If i use this then i get a compile time error.
and the error is = is not overload for MyArray
Post by David Wilkinson
IMO, a better design would be
class MyArray:public CArray<CString,CString&>
{
};
I can't use CArray<CString,CString&>. Because i want to take both strings
in CArray. If i use CArray<CString,CString&> then second parameter will be
a reference.
Post by David Wilkinson
void function1(MyArray& obj)
{
//I add some strings to obj
}
MyArray obj;
function1(obj);
This avoids all copying of MyArray objects.
Yes this is better approach which everybody follow. But i was asking about
returning CArray from a function.


Thanks
David Wilkinson
2006-05-23 13:00:51 UTC
Permalink
MFCQuery wrote:

Inline:

[snip]
Post by MFCQuery
Post by David Wilkinson
MyArray Obj = function1();
This statement is not valid. If i use this then i get a compile time error.
and the error is = is not overload for MyArray
This I can believe. You would need to write an override of the
assignment operator.
Post by MFCQuery
Post by David Wilkinson
IMO, a better design would be
class MyArray:public CArray<CString,CString&>
{
};
I can't use CArray<CString,CString&>. Because i want to take both strings
in CArray. If i use CArray<CString,CString&> then second parameter will be
a reference.
This I do not understand. Using a reference for second template
parameter is the "preferred way" of using CArray.
Post by MFCQuery
Post by David Wilkinson
void function1(MyArray& obj)
{
//I add some strings to obj
}
MyArray obj;
function1(obj);
This avoids all copying of MyArray objects.
Yes this is better approach which everybody follow. But i was asking about
returning CArray from a function.
Actually you asked about returning a reference (at least in the title).
I am surprised that your posted function1 (which returns by value) will
compile, because it requires a copy constructor for CArray.

But really, IMO, these MFC template collection classes are very badly
designed. Your life will become so much easier if you use std::vector.
For example

1. No confusing double template argument.

2. Copy constructor and assignment operator are automatically defined.

3. If you wish, you can do

#include <vector>

typedef std::vector<CString> MyArray;

MyArray function1()
{
MyArray obj;
// add some strings to obj and return it
return obj;
}

MyArray obj = function1();

However, it will still be more efficient to define your function as

void function1(MyArray& obj)
{
//add some strings to obj
}

David Wilkinson
MFCQuery
2006-05-24 04:50:07 UTC
Permalink
Hi,

Thanks for telling about std::vector.
Post by David Wilkinson
[snip]
Post by MFCQuery
Post by David Wilkinson
MyArray Obj = function1();
This statement is not valid. If i use this then i get a compile time error.
and the error is = is not overload for MyArray
This I can believe. You would need to write an override of the
assignment operator.
I have tried to override an assignment operator but everytime i got some
error. Can you tell me how to write or please give any link on net.

Is there any default method to sort the elements of CArray? If you know
then please give some links for CArray because I also have a problem
regarding CArray.

In a structure, if i use CArray and make link list of objects of structure
then i got run time error. Like

typedef struct xyz
{
int a;
char b;
CArray<string,string>MyArray;
}XYZ;

XYZ *startpoint.
When i allocate memory to startpoint and fill values to its element then i
gor error.
startpoint=(XYZ*)malloc(sizeof(XYZ));
startpoint.a=0;
startpoint.b='0';
startpoint.MyArray.Add("string1"); //this line give error


Thanks
David Wilkinson
2006-05-24 11:51:08 UTC
Permalink
Post by MFCQuery
Hi,
Thanks for telling about std::vector.
[snip]
Post by MFCQuery
I have tried to override an assignment operator but everytime i got some
error. Can you tell me how to write or please give any link on net.
Is there any default method to sort the elements of CArray? If you know
then please give some links for CArray because I also have a problem
regarding CArray.
In a structure, if i use CArray and make link list of objects of structure
then i got run time error. Like
typedef struct xyz
{
int a;
char b;
CArray<string,string>MyArray;
}XYZ;
XYZ *startpoint.
When i allocate memory to startpoint and fill values to its element then i
gor error.
startpoint=(XYZ*)malloc(sizeof(XYZ));
startpoint.a=0;
startpoint.b='0';
startpoint.MyArray.Add("string1"); //this line give error
Thanks
MFCQuery:

1. When you post, please post the actual code, don't retype. Clearly,
this code is not what you have because, for example

startpoint.a=0;

will not compile because startpoint is a pointer.

2. When you get an error, post the error message together with the code
that caused it.

3. Why are you using malloc in a C++ program? Always use new. But here
you almost certainly need new either. Just do

XYZ startpoint.
startpoint.a=0;
startpoint.b='0';
startpoint.MyArray.Add("string1"); //this line give error

4. What error?

5. If you use std::vector, then you can use std::sort(). Believe me, get
rid of these MFC collection classes and learn about the standard library.

David Wilkinson

Loading...