Re: Is it possible to assign a default value...great, but how?

Tech-Archive recommends: Speed Up your PC by fixing your registry

From: John Carson (donaldquixote_at_datafast.net.au)
Date: 04/18/04


Date: Sun, 18 Apr 2004 19:03:11 +1000


"David F" <David-White@earthlink.net> wrote in message
news:55qgc.18443$A_4.15300@newsread1.news.pas.earthlink.net
> My understanding from your answer to the question "Is it possible..."
> in one word is simply... NO.

Correct.

> As far as the type X is concerned, I would hope that it is clear that
> it does NOT matter what type is it, whether int or class, for example.
> The key point and the clarification I made following immediately my
> original posting, is as follows:
> "I meant of course a default value that addresses no real object,
> much as NULL does it with pointers".

I did not notice your clarification owing to your decision to put it in the
subject line rather than the body of your post. You might be interested to
know that your changes of subject have caused this thread to be split into 3
separate threads on Google Groups. I mention this not because I actually
read your posts in Google Groups but as one reason why people don't normally
change subject lines and hence one reason why readers of posts don't look to
subject line changes as sources of information.

> While for pointers, again, regardless of the type of X I can write:
> "void f(X* p = NULL) { }", where X doesn't have to exist as an actual
> object,

What you mean is that NULL is not an address of an X object.

> and the meaning is that p points to nothing, Consequently, I
> can call f like this: f().

You can call f like that without the availability of a NULL.

> I was looking for a similar / parallel answer for a reference: "void
> f(X& r = NOTHING) { }", where X doesn't exists as an actual object

What you mean is that NOTHING is not an X object.

> and r references or is the address of nothing. The term NOTHING is
> of course my invention just to demonstrate my point. So again, I
> could call f() in the case of a reference argument too.

You can call f() without there being a NOTHING in the language.

> Logically speaking, there is no reason not to have it, much the same
> way we have it fot pointers.
>

The other side of making NOTHING available for those who want it is that
everybody would then have to test that r!= NOTHING before performing
operations on r. The existing arrangement offers something to everyone;
those who need to be able to use NULL can use pointers, those who don't want
to bother testing for whether they are dealing with something non-NULL can
use references. Another way around the problem is to use function
overloading, i.e., define a separate

void f()
{
    // stuff
}

in addition to

void f(X& r)
{
    // stuff
}

-- 
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)


Relevant Pages

  • Re: Pointers vs References: A Question on Style
    ... void funk(SimCity const * pCity) ... > pointers when it comes to parameter passing. ... C++ functions can take arguments by copy, by address, or by reference. ...
    (comp.lang.cpp)
  • Re: CALL FOR COMMENT ON NULL: Re: what is typecasting a pointer to the type (void *)p mean?
    ... > The null value for pointers is only useful for setting pointers that ... The reason is that, while most of the time NULL is defined ... Please read section 5 of the C FAQ, ... You can't have an object of type void. ...
    (comp.lang.c)
  • Re: previous declaration of Table was here / conflicting types for
    ... it can be casted to any other type of pointers. ... struct Table, because I need to implement the members of Table. ... He has some sample codes where List wastypedef'd void pointer but the ... Your professor may have a reason for doing this, ...
    (comp.lang.c)
  • Re: Funny old Laws
    ... It's an old City ordinance which for some reason ... therefore be void. ... But I will seek out a reference. ... Thats about all ...
    (uk.legal)
  • Re: Structures, Pointer and gets
    ... > void or you can use it void but call by reference ... There is no such thing as call by reference in C. ... As it happens, he's using an array, so it's a moot point anyway (since the ... Arrays are just pointers for C. ...
    (comp.lang.c)