Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Pass by reference but with default parameter

Reply
Thread Tools

Pass by reference but with default parameter

 
 
Manu
Guest
Posts: n/a
 
      09-22-2009
Hi,

Check the following code.


bool Func1(A* ptr = NULL)
{
ptr = Func2();
}

void main()
{
A* ptr1;
Func1(ptr1);
}

In the above case the value of ptr1 will be some junk value.
As a result i modified the code like

bool Func1(A*& ptr)
{
ptr = Func2();
}

Now it works fine.

But I need a function with a default parameter as NULL but at the same time
the parameter has to be passed as reference.
How can I achieve it??

Regards,
R Manu


 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      09-22-2009
Manu wrote:
> Hi,
>
> Check the following code.
>
>
> bool Func1(A* ptr = NULL)
> {
> ptr = Func2();
> }


That really is a silly idea, what happens if you call Func1() ?

> void main()
> {
> A* ptr1;
> Func1(ptr1);
> }
>
> In the above case the value of ptr1 will be some junk value.
> As a result i modified the code like
>
> bool Func1(A*& ptr)
> {
> ptr = Func2();
> }
>
> Now it works fine.
>
> But I need a function with a default parameter as NULL but at the same time
> the parameter has to be passed as reference.


Why?

> How can I achieve it??


You can't have a null reference.

--
Ian Collins
 
Reply With Quote
 
 
 
 
Francesco S. Carta
Guest
Posts: n/a
 
      09-22-2009
On 22 Set, 12:54, "Manu" <(E-Mail Removed)> wrote:
> Hi,
>
> Check the following code.
>
> bool Func1(A* ptr = NULL)
> {
> * * ptr = Func2();
>
> }


First off, you missed to return an appropriate value, and secondarily,
it would be better to use the 0 (zero) literal instead of NULL. But
that's not what you're asking for.

>
> void main()
> {
> * * A* ptr1;
> * * Func1(ptr1);
>
> }
>
> In the above case the value of ptr1 will be some junk value.


And being junk, you shouldn't pass it around.
If you want a pointer to something but you can't appropriately set it
in the declaration, initialize it to 0.

> As a result i modified the code like
>
> bool Func1(A*& ptr)
> {
> * * ptr = Func2();
>
> }
>
> Now it works fine.
>
> But *I need a function with a default parameter as NULL but at the same time
> the parameter has to be passed as reference.
> How can I achieve it??


As Ian pointed out, you cannot have a null reference.

If you detail better your needs and your aims you could get some good
advice about the implementation. What exactly are Func1 and Func2
meant to do?

Have good time,
Francesco
--
Francesco S. Carta, hobbyist
http://fscode.altervista.org
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      09-22-2009
On Sep 22, 12:19 pm, "Francesco S. Carta" <(E-Mail Removed)> wrote:
> On 22 Set, 12:54, "Manu" <(E-Mail Removed)> wrote:


> > bool Func1(A* ptr = NULL)
> > {
> > ptr = Func2();
> > }


> First off, you missed to return an appropriate value,


Not to mention that he's modifying a local variable.

> and secondarily, it would be better to use the 0 (zero)
> literal instead of NULL.


Why? Everywhere I've worked, using NULL has been preferred.

> > void main()


Just a nit, but "void main()" won't compile with a conformant
compiler. The function main must return int.

> > {
> > A* ptr1;
> > Func1(ptr1);
> > }


> > In the above case the value of ptr1 will be some junk value.


> And being junk, you shouldn't pass it around.
> If you want a pointer to something but you can't appropriately
> set it in the declaration, initialize it to 0.


> > As a result i modified the code like


> > bool Func1(A*& ptr)
> > {
> > ptr = Func2();
> > }


> > Now it works fine.


> > But I need a function with a default parameter as NULL but
> > at the same time the parameter has to be passed as
> > reference.
> > How can I achieve it??


> As Ian pointed out, you cannot have a null reference.


> If you detail better your needs and your aims you could get
> some good advice about the implementation. What exactly are
> Func1 and Func2 meant to do?


He can write something like:

bool Func1( A* const& ptr = NULL ) ;

If he needs a non-const reference, something like:

extern A* defaultParam ;
bool Func1( A* const& ptr = defaultParam ) ;

will work, but he really has to make sure that he doesn't
actually modify the defaultParam somewhere (since it's not
const).

--
James Kanze
 
Reply With Quote
 
Francesco S. Carta
Guest
Posts: n/a
 
      09-22-2009
On 22 Set, 16:30, James Kanze <(E-Mail Removed)> wrote:
> On Sep 22, 12:19 pm, "Francesco S. Carta" <(E-Mail Removed)> wrote:
>
> > On 22 Set, 12:54, "Manu" <(E-Mail Removed)> wrote:
> > > bool Func1(A* ptr = NULL)
> > > {
> > > * * ptr = Func2();
> > > }

> > First off, you missed to return an appropriate value,

>
> Not to mention that he's modifying a local variable.


Missed that...

> > and secondarily, it would be better to use the 0 (zero)
> > literal instead of NULL.

>
> Why? *Everywhere I've worked, using NULL has been preferred.


Just because my reference book (TC++PL) suggests so.

A rationale is given - something along the lines that a pure zero
could lead to less problems, due to the stricter C++ type checking,
but don't ask me to support that rationale, it's outside of my
knowledge: I just trusted the author.

Of course, were I to work on a team/project where NULL is expected to
be used, I would use it without any complain, even though I dislike
it.

> > > void main()

>
> Just a nit, but "void main()" won't compile with a conformant
> compiler. *The function main must return int.


Completely overlooked that... never mind.

Have good time,
Francesco
--
Francesco S. Carta, hobbyist
http://fscode.altervista.org
 
Reply With Quote
 
Balog Pal
Guest
Posts: n/a
 
      09-22-2009
"James Kanze" <(E-Mail Removed)>

>> and secondarily, it would be better to use the 0 (zero)
>> literal instead of NULL.

>
> Why? Everywhere I've worked, using NULL has been preferred.


0 is always there. To have NULL you must #include something.



 
Reply With Quote
 
Francesco S. Carta
Guest
Posts: n/a
 
      09-22-2009
On 22 Set, 18:20, Victor Bazarov <(E-Mail Removed)> wrote:
> Balog Pal wrote:
> > "James Kanze" <(E-Mail Removed)>

>
> >>> and secondarily, it would be better to use the 0 (zero)
> >>> literal instead of NULL.
> >> Why? *Everywhere I've worked, using NULL has been preferred.

>
> > 0 is always there. To have NULL you must #include something.

>
> Yes, that's true. *In production environment, however, standard headers
> are pretty much always included (directly or indirectly) which basically
> makes "NULL" pretty much "always there" as well.
>
> Another reason to prefer 'NULL' - it's much more effective to do the
> search-and-replace on "NULL" than on "0" when time comes to switch to
> using 'nullptr' (the new keyword, when your compiler starts supporting it).


Interesting. Just out of curiosity, am I allowed to define and use
"const int nullptr = 0;" in my code?

I know that it seems that I'm contradicting myself, but the idea of
being able to spot all nullified pointers with a search is indeed
useful, while I still dislike using the NULL macro.

Well, I could define "const int NULL = 0;" as suggested by my book...
I don't know, whatever.

Have good time,
Francesco
--
Francesco S. Carta, hobbyist
http://fscode.altervista.org
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      09-22-2009
Francesco S. Carta wrote:
> [..] Just out of curiosity, am I allowed to define and use
> "const int nullptr = 0;" in my code?


Right now you are, of course. After C++0x support is introduced in the
compiler you use, it's likely to fail, 'nullptr' is going to be a new
keyword (see table 3), and you can't use a keyword as the name of a
variable.

> I know that it seems that I'm contradicting myself, but the idea of
> being able to spot all nullified pointers with a search is indeed
> useful, while I still dislike using the NULL macro.
>
> Well, I could define "const int NULL = 0;" as suggested by my book...


Only if you never include any of the standard headers that might lead to
the definition of the macro 'NULL'... A tall order, AFAICT.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
Jerry Coffin
Guest
Posts: n/a
 
      09-22-2009
In article <h9aad9$pa3$(E-Mail Removed)>,
http://www.velocityreviews.com/forums/(E-Mail Removed) says...
>
> Hi,
>
> Check the following code.
>
>
> bool Func1(A* ptr = NULL)
> {
> ptr = Func2();
> }


For this to accomplish anything useful, it probably needs to be
something like:
*ptr = Func();

A default should usually be a 'in' parameter -- one that specifies
HOW the function does its job, but for which there is a reasonable
default behavior. Otherwise, you're typically looking at a function
that really does two different things, which is a problem in itself
(there are a few exceptions, but they're pretty unusual).

A reference, however, signals an 'inout' parameter -- one that's
modified by the function in question. To make it an 'in', you'd want
a reference to const.

For this case, you can simply define an object to be used as the
default, and then compare to that when needed:

const A default_value;

bool Func1(A const &value = default_value) {
use(value);
}

--
Later,
Jerry.
 
Reply With Quote
 
Francesco S. Carta
Guest
Posts: n/a
 
      09-22-2009
On 22 Set, 19:22, Victor Bazarov <(E-Mail Removed)> wrote:
> Francesco S. Carta wrote:
> > [..] *Just out of curiosity, am I allowed to define and use
> > "const int nullptr = 0;" in my code?

>
> Right now you are, of course. *After C++0x support is introduced in the
> compiler you use, it's likely to fail, 'nullptr' is going to be a new
> keyword (see table 3), and you can't use a keyword as the name of a
> variable.


Ah, very good to know! When my compiler will support it I'll have just
to remove that declaration and the code will be ready to go.

> > I know that it seems that I'm contradicting myself, but the idea of
> > being able to spot all nullified pointers with a search is indeed
> > useful, while I still dislike using the NULL macro.

>
> > Well, I could define "const int NULL = 0;" as suggested by my book...

>
> Only if you never include any of the standard headers that might lead to
> the definition of the macro 'NULL'... *A tall order, AFAICT.


Yes, a tall order indeed. I'll go for nullptr in my code.

Thanks a lot,
have good time,
Francesco
--
Francesco S. Carta, hobbyist
http://fscode.altervista.org
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
decltype as a template parameter when containing reference to anothertemplate parameter. Isti C++ 2 04-19-2010 10:01 PM
How to pass a parameter for a function parameter in a function AzamSharp Javascript 2 07-05-2008 12:24 AM
Using declaration inside first template parameter as default valuefor second template parameter. Stuart Redmann C++ 5 12-14-2007 08:42 AM
Finding the memory address of a pass-by-reference parameter? scott.lewis@gmail.com C++ 3 04-30-2005 06:13 PM
Is it possible to pass a parameter by reference? Anthony Liu Python 3 02-27-2005 08:03 AM



Advertisments