clayton@thumper.bellcore.com (R. Clayton) (01/12/90)
The following code appears in the middle of page 107 in Dewhurst and
Stark's "Programming in C++":
extern char *home_dir, *path, *file;
Pathname home = home_dir;
Pathname file = home + path + file;
FILE *fp = file->open();
Is this code correct? Can variables (i.e., file) be overloaded?
Assuming the final reference to file is as a Pathname, shouldn't the
access operator be "." instead of "->"? Running versions of this code
through g++ 1.36.1 result in the expected compilation errors.
R. Clayton
clayton@thumper.bellcore.com
{rutgers!}bellcore!thumper!claytonark@alice.UUCP (Andrew Koenig) (01/14/90)
In article <1794@thumper.bellcore.com>, clayton@thumper.bellcore.com (R. Clayton) writes: > extern char *home_dir, *path, *file; > Pathname home = home_dir; > Pathname file = home + path + file; > FILE *fp = file->open(); > Is this code correct? Can variables (i.e., file) be overloaded? Variables cannot be overloaded. It looks like a typo -- It looks like it should work if you change the name of one of the instances of `file' systematically: extern char *home_dir, *path, *file; Pathname home = home_dir; Pathname file1 = home + path + file; FILE *fp = file1->open(); One might think the following might work too: extern char *home_dir, *path, *file; Pathname home = home_dir; { Pathname file = home + path + file; FILE *fp = file->open(); // ... } with `file' in `home + path + file' referring to the `file' in the enclosing scope. Unfortunately, the ANSI C standard decrees that a variable is defined from the instant its name is uttered in its declaration and C++ goes along with that. Thus int x = 5; { int x = x + 1; // ... } Here the inner x is NOT set to 6 (outer x + 1) but instead is undefined. -- --Andrew Koenig ark@europa.att.com