Problem with "delete" and what about "friend" classes & functions?
Sean Kelly
sean at f4.ca
Fri Jan 5 15:56:14 PST 2007
John Kiro wrote:
> Hello
>
> I have a problem with this statement:
>
> delete pDoc.getFile();
>
> It generates this error:
>
> "Error: ((cast(CDoc)(pDoc)).getFile)() is not an lvalue"
>
> The getFile() is a member of CDoc (pDoc is an object of a CDoc
> derived class), and is defined like this:
>
> CFile getFile()
> {
> return m_pFile;
> }
>
> So any idea about the reason of this error?
Pretend that delete is actually a function declared like this:
void delete( inout void* val )
{
free( val );
val = null;
}
rvalues (ie. temporaries) cannot be passed to functions by reference
because they don't have a concrete address (basically). So you first
have to store the result in an lvalue (a normal variable) before calling
delete on it.
> The code compiles successfully if I replace the delete statement with:
>
> CFile tmpFile=pDoc.getFile();
> delete tmpFile;
>
> Another point, I guess D doesn't support "friend" classes or
> functions.. wouldn't they be useful sometimes?
They are. But "friend" as a keyword isn't necessary in D because of the
way visibility specifiers work. "private" actually means "visible to
everything in the current module" and "package" means "visible to
everything in the current package." And "protected" means "visible to
everything in the current module (I think) and to derived classes." So
in essence, you can think of all class members as automatically being
friends with one another.
Sean
More information about the Digitalmars-d-learn
mailing list