andyw27
asked on
Maxium number of values returnable by single fucntion ?
How many values can a function return ?
I have a function which accepts several pointers. It's return type is int*
If I had the following would both be returned ?
(*value01)++;
return value01;
(*value02)++;
return value02;
Or do I even need to specify a return type because the pointers would be changed whether I returned them or not ?
Thanks.
I have a function which accepts several pointers. It's return type is int*
If I had the following would both be returned ?
(*value01)++;
return value01;
(*value02)++;
return value02;
Or do I even need to specify a return type because the pointers would be changed whether I returned them or not ?
Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
As it is C++ I would suggest to create a class like
class CCounters
{
public:
CCounters(): m_Value1(0), m_Value2(0) {}
void IncValue1() {m_Value1++;}
void IncValue2() {m_Value2++};
void IncAll() {IncValue1(); IncValue2();}
private:
int m_Value1;
int M_Value2;
}
...
CCounters Counters;
..
func(Counters);
...
void func(CCounter& rCounters)
{
....
rCounters.IncAll();
}
class CCounters
{
public:
CCounters(): m_Value1(0), m_Value2(0) {}
void IncValue1() {m_Value1++;}
void IncValue2() {m_Value2++};
void IncAll() {IncValue1(); IncValue2();}
private:
int m_Value1;
int M_Value2;
}
...
CCounters Counters;
..
func(Counters);
...
void func(CCounter& rCounters)
{
....
rCounters.IncAll();
}
or another function could be like;
int* returnSomething(int* p1,int* p2)
{
int* ret=new int[5];
p1++;
p2++;
for (int i=0;i<5;i++)
*(ret+i)=i;
return ret;
}
Here both p1 and p2 is modified and you also get five different value, or as much as you can
To use it;
int main()
{
x=5;y=7;
int* pf=returnSomething(&x,&y);
//now x and y are both incremented
for (int i=0;i<5;i++)
std::cout<<*(pf+i)<<std::e ndl;
return
}
Regards,
int* returnSomething(int* p1,int* p2)
{
int* ret=new int[5];
p1++;
p2++;
for (int i=0;i<5;i++)
*(ret+i)=i;
return ret;
}
Here both p1 and p2 is modified and you also get five different value, or as much as you can
To use it;
int main()
{
x=5;y=7;
int* pf=returnSomething(&x,&y);
//now x and y are both incremented
for (int i=0;i<5;i++)
std::cout<<*(pf+i)<<std::e
return
}
Regards,
In addition you have the memory leak.
I'm sorry, but the last suggestion I would be never recommended because it is a "dirty" solution.
I think the answer coud be:
The function can return only one value as return. This value can be a value, a pointer or a reference.
So we can return the complex object and this object can hold as many values as we need.
I'm sorry, but the last suggestion I would be never recommended because it is a "dirty" solution.
I think the answer coud be:
The function can return only one value as return. This value can be a value, a pointer or a reference.
So we can return the complex object and this object can hold as many values as we need.
>>In addition you have the memory leak.
int main()
{
x=5;y=7;
int* pf=returnSomething(&x,&y);
//now x and y are both incremented
for (int i=0;i<5;i++)
std::cout<<*(pf+i)<<std::e ndl;
delete pf; //delete the memory pointed by pf
return 0;
}
Would you still have the memory leaks?
int main()
{
x=5;y=7;
int* pf=returnSomething(&x,&y);
//now x and y are both incremented
for (int i=0;i<5;i++)
std::cout<<*(pf+i)<<std::e
delete pf; //delete the memory pointed by pf
return 0;
}
Would you still have the memory leaks?
In this case not any more but in real project it is very easy to forget it.
>>> but in real project it is very easy to forget it.
Yes, normally you should delete a pointer where you created it. Only *create* functions where the only purpose is to create a new pointer are an exception from above rule. In a class you should store newly created pointers (if there is a need for pointers at all) in a data member and delete in the destructor.
Regards, Alex
Yes, normally you should delete a pointer where you created it. Only *create* functions where the only purpose is to create a new pointer are an exception from above rule. In a class you should store newly created pointers (if there is a need for pointers at all) in a data member and delete in the destructor.
Regards, Alex
ASKER
Thanks thats a great answer.