Can I call the function in this way

Q

QQ

I have a function defined in CC file like
int D(char* p, char* e, char* c,char* g1, short g1p, char* g2, short
g2p, bool s );

I put it in the header file as
int D(char* p, char* e, char* c, char* g1, short g1p, char* g2, short
g2p, bool s = false);


When I call it in the CC can I just use this format, where I don't pass
bool s
D(p, e, c, g1, g1p, g2, g2p);

Thanks a lot!
 
R

Rapscallion

QQ said:
I have a function defined in CC file like
int D(char* p, char* e, char* c,char* g1, short g1p, char* g2, short
g2p, bool s );

I put it in the header file as
int D(char* p, char* e, char* c, char* g1, short g1p, char* g2, short
g2p, bool s = false);


When I call it in the CC can I just use this format, where I don't pass
bool s
D(p, e, c, g1, g1p, g2, g2p);

No, you will get an 'ambigous function call' error.
 
L

Lou Pecora

QQ said:
I have a function defined in CC file like
int D(char* p, char* e, char* c,char* g1, short g1p, char* g2, short
g2p, bool s );

I put it in the header file as
int D(char* p, char* e, char* c, char* g1, short g1p, char* g2, short
g2p, bool s = false);


When I call it in the CC can I just use this format, where I don't pass
bool s
D(p, e, c, g1, g1p, g2, g2p);

Thanks a lot!

Looks right.

-- Lou Pecora (my views are my own) REMOVE THIS to email me.
 
K

Kristo

QQ said:
I have a function defined in CC file like
int D(char* p, char* e, char* c,char* g1, short g1p, char* g2, short
g2p, bool s );

I put it in the header file as
int D(char* p, char* e, char* c, char* g1, short g1p, char* g2,
short g2p, bool s = false);


When I call it in the CC can I just use this format, where I don't
pass bool s
D(p, e, c, g1, g1p, g2, g2p);

Thanks a lot!

Yes. That's why C++ allows functions to have default arguments. A
good C++ book should cover this in more detail.

Kristo
 
H

Howard

I have a function defined in CC file like
int D(char* p, char* e, char* c,char* g1, short g1p, char* g2, short
g2p, bool s );

Is this really what you have? That isn't a function definition, it's just a
function prototype. Do you really have something like this?

int D(char* p, char* e, char* c,char* g1, short g1p, char* g2, short g2p,
bool s )
{
// function code here...
}

That's a function definition. If that's what you have, then you're fine.

BUT!...if you have a function prototype here, as you've posted, and ALSO a
function prototype like this one below in the header file:
int D(char* p, char* e, char* c, char* g1, short g1p, char* g2, short
g2p, bool s = false);

....then (assuming you #include the header file) the compiler won't know
which function to call.

(We aren't sure which way your code is written. That's why you've gotten
two different answers.)

-Howard
 
P

Pete Becker

Howard said:
(We aren't sure which way your code is written. That's why you've gotten
two different answers.)

No, the reason is much more basic: this area is confusing. <g>

These two prototypes describe the same function:

void f(char *p, bool s = false);
void f(char *p, bool s);

In any translation unit that has the first one you can call f with one
argument (and the default value) or with two. In any translation unit
that has only the second one you can only call f with two arguments.

In the translation unit that defines f, if it #includes the header with
the first prototype (as it should), then the definition of f cannot have
the default argument. So it's reasonable to say that in that translation
unit f is defined as "void f(char *p, bool s);". Yes, it's not
technically a definition, but newsgroup readers aren't compilers, and
it's okay to speak shorthand now and then.

The second of these two prototypes is illegal:

void f(char *p, bool s = false);
void f(char *p, bool s = false);

because it redefines the default argument. That's also the reason the
actual definition can't have a default argument if the translation unit
has the first prototype: it would also redefine the default argument.
 
L

Larry I Smith

QQ said:
I have a function defined in CC file like
int D(char* p, char* e, char* c,char* g1, short g1p, char* g2, short
g2p, bool s );

I put it in the header file as
int D(char* p, char* e, char* c, char* g1, short g1p, char* g2, short
g2p, bool s = false);


When I call it in the CC can I just use this format, where I don't pass
bool s
D(p, e, c, g1, g1p, g2, g2p);

Thanks a lot!

If this is what you mean:

// --- stuff.h
int D(char* p, char* e, char* c, char* g1, short g1p, char* g2,
short g2p, bool s = false);


// --- stuff.cpp
#include "stuff.h"

int D(char* p, char* e, char* c,char* g1, short g1p, char* g2,
short g2p, bool s)
{
// do something
}


// --- morestuff.cpp
#include "stuff.h"
..
..
bool ok = false;

// this should work just fine. 's' will be false when D()
// is invoked
D(p, e, c, g1, g1p, g2, g2p);
// these also work
D(p, e, c, g1, g1p, g2, g2p, true);
D(p, e, c, g1, g1p, g2, g2p, false);
D(p, e, c, g1, g1p, g2, g2p, ok);


Regards,
Larry
 
J

Jaspreet

QQ said:
I have a function defined in CC file like
int D(char* p, char* e, char* c,char* g1, short g1p, char* g2, short
g2p, bool s );

This means you have something like
int D(char* p, char* e, char* c,char* g1, short g1p, char* g2, short
g2p, bool s )
{
--
}
I am asking just because in your post you have put a ;(semicolon) after
the function header. The ; should only be there for prototype and
should not be there for function definition.
I put it in the header file as
int D(char* p, char* e, char* c, char* g1, short g1p, char* g2, short
g2p, bool s = false);

This is the prototype of the function and you have correctly given a
default value to an argument. Remember that you can give deault values
on ly to the rightmost arguments.
So, while 1 is correct, 2nd proto is wrong:

1. int func(int p, int v=3, int u=4);
2. int func(int p, int v=3, int u);
When I call it in the CC can I just use this format, where I don't pass
bool s
D(p, e, c, g1, g1p, g2, g2p);

Yes when you make a call to the function you can omit the last
argument. In this case the last argument would go as false. Incase, you
wanted to pass some other value you can always over-ride the default
value.
 

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
474,203
Messages
2,571,059
Members
47,668
Latest member
SamiraShac

Latest Threads

Top