Topic: WHat is wrong with th efollowing..
Author: sampsona@ifi.uio.no (S. D. A.)
Date: 7 Nov 1994 12:18:25 +0100 Raw View
Hi:
I have a code which is suppose to return a pointer to a class
as follows.. Kindly commnent on the return value of Sample::interRot.
thank you
class Sample {
public:
Sample(int n) : len_(n); //ctor
~Sample(); //dtor
Sample(const Sample& s); //copy ctor
elem *interRot(const int how);
private:
elem *list_;
int len_
};
class elem {
int value;
};
elem* Sample::interRot(const int how)
{
elem a[how];
for ( int ch = 0; ch < len_; ++ch)
a[ch] = list_[ch];
.
.
.
return(a);
}
--
sampsona@ifi.uio.no
Seeds, like ideas, don't germinate in concrete
Author: esap@kaarne.cs.tut.fi (Pulkkinen Esa)
Date: 8 Nov 1994 09:15:37 GMT Raw View
S. D. A. (sampsona@ifi.uio.no) wrote:
: Hi:
:
: I have a code which is suppose to return a pointer to a class
: as follows.. Kindly commnent on the return value of Sample::interRot.
: thank you
(class definitions deleted)
: elem* Sample::interRot(const int how)
: {
: elem a[how];
:
: for ( int ch = 0; ch < len_; ++ch)
: a[ch] = list_[ch];
: .
: .
: .
: return(a);
:
: }
The array a is local to the function, so returning the pointer is
wrong. Use dynamically allocated array instead.
--
Esa Pulkkinen | C++ programmers do it virtually
E-Mail: esap@cs.tut.fi | everywhere with a class, resulting
WWW : http://www.cs.tut.fi/~esap/ | in multiple inheritance.