C++: Uninitialised Variable Passed as a Parm

J

Jason Heyes

E. Robert Tisdale said:
Greg said:
That's a very good rule of thumb, but if I'm understanding you,
forcing an initialization can often be awkward.

Not very often
but I'll give an example where it is awkward -- containers:

int a[n];
for (int j = 0; j < n; ++j)
cin >> a[n];

Unless they are small, const containers can be awkward to initialize:

const int a[] = {0, 1, 2, 3};

Well, you get the idea. You can do this:

class wrapper {
private:
int a[n];
public:
const
int& operator[](int j) const { return a[j]; }
int& operator[](int j) { return a[j]; }
wrapper(std::istream& is) {
for (int j = 0; j < n; ++j)
is >> a[n];
}
};

then this:

const wrapper a(cin);

Of course, this can cause a problem,
if the constructor encounters an exception while reading from cin.
Is this awkward? Problematic?
I suspect that you would say yes.

To get around this you can write

std::istream &operator>>(std::istream &is, wrapper *&p)
{
int a[n];
for (int j=0; j < n; j++)
{
if (!(is >> a[j]))
return is;
}
p = new wrapper(a);
return is;
}

then

wrapper *p;
if (!(cin >> p))
// error
wrapper a = *p;
delete p;

OR you can use std::vector instead of arrays!
 
E

E. Robert Tisdale

Jason said:
To get around this,

Please remind me what we are trying to "get around".
you can write:

std::istream &operator>>(std::istream& is, wrapper& w) {
int j = 0;
while (j < n && is >> w[j]) ++j;
return is;
}

then

wrapper* p;
if (!(std::cin >> *p)) {
/* handle error */ }
const wrapper w = *p;
delete p;
p = NULL;

But a null pointer variable p is *not* much of a safety improvement.
How about

wrapper f(std::istream& is) {
wrapper w;
is >> w;
return w;
}

then

const wrapper w = f(std::cin);
if (!cin) {
/* handle error */ }
 
J

Jeffrey Schwab

E. Robert Tisdale said:
Jason said:
To get around this,


Please remind me what we are trying to "get around".
you can write:

std::istream &operator>>(std::istream& is, wrapper& w) {
int j = 0;
while (j < n && is >> w[j]) ++j;
return is;
}

then

wrapper* p;
if (!(std::cin >> *p)) {

Now, E. Robert, that is *not* what Jason wrote. He wrote:

if (!(cin >> p))

I find this distasteful, and it really doesn't solve anything. However,
I don't believe it is technically wrong.
But a null pointer variable p is *not* much of a safety improvement.

Even in your modified version, p was not (necessarily) null.

How about

wrapper f(std::istream& is) {
wrapper w;
is >> w;
return w;
}

You're throwing wrappers around as if they were candy. Keep in mind
that these are wrappers around arrays that were, by definition, too
large to initialize like this:

const int a[] = {0, 1, 2, 3};

Those copies could hurt.
then

const wrapper w = f(std::cin);
if (!cin) {
/* handle error */ }

Suppose the wrapper had to read the entire input stream, through EOF?
This code makes that an error.

Unless there's a darned good, concrete reason not to, initialize in
constructors. /Resource allocation is initialization./ EOT.

-Jeff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top