By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 810 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

Replace a #define by a method

P: n/a
Dear all,

I am a beginner of C/C++ programmation and I don't understand how the #define command works.

I would like to make this statement

#define MAX2(V1, V2) (V1>V2 ? V1 : V2)

as a C routine in order to use it in other C programs. However, I don't know what type to put for the retourning value.

Could you help me, please?

thanks a lot,

Marcelo
Dec 13 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Marcelo wrote:
Dear all,

I am a beginner of C/C++ programmation and I don't understand how the
#define command works.

I would like to make this statement

#define MAX2(V1, V2) (V1>V2 ? V1 : V2)

as a C routine in order to use it in other C programs. However, I don't
know what type to put for the retourning value.

Could you help me, please?

thanks a lot,

Marcelo


Hello Marcelo,

#define is a pre-processor directive. Perhaps you want a normal
function, e.g.:
int MAX2(const int& V1, const int& V2)
{
return std::max(V1,V2);
}
with int replaced by the type you want, or perhaps a template?

Regards,
Peter Jansson
Dec 13 '05 #2

P: n/a
On Tue, 13 Dec 2005 22:47:17 +0100 in comp.lang.c++, Marcelo
<ma********@yahoo.com> wrote,
I am a beginner of C/C++ programmation and I don't understand how the #define command works.
Greetings. Since the answers are often different for C vs. C++ you
should make up your mind which one you want to use. In comp.lang.c++
it's C++.

#define works just as if the expanded text were written right into the
source at the point where it is invoked.
I would like to make this statement

#define MAX2(V1, V2) (V1>V2 ? V1 : V2)

as a C routine in order to use it in other C programs. However, I don't know what type to put for the retourning value.


In C++, you should use std::max().
The return type is the same as the argument type.

In C, the #define macro is passably OK and you can use it in multiple
programs by putting it in a header that you include, just as easily as
if it were a function. Beware of issues such as arguments with side
effects.

Dec 13 '05 #3

P: n/a
Marcelo wrote:
Dear all,

I am a beginner of C/C++ programmation and I don't understand how the
#define command works.

I would like to make this statement

#define MAX2(V1, V2) (V1>V2 ? V1 : V2)

as a C routine in order to use it in other C programs. However, I don't
know what type to put for the retourning value.

Could you help me, please?

thanks a lot,

Marcelo

Great ideas and explanations, thanks
Dec 13 '05 #4

P: n/a
Marcelo wrote:
Dear all,

I am a beginner of C/C++ programmation and I don't understand how the
#define command works.

I would like to make this statement

#define MAX2(V1, V2) (V1>V2 ? V1 : V2)

as a C routine in order to use it in other C programs. However, I don't
know what type to put for the retourning value.

Could you help me, please?

thanks a lot,

Marcelo

The STL libraries are based on templates and yes std::max() should fit
the bill. If you're likely to need generic classes and functions that
are based on specific types then I'd recommend you learn more about
template programming.

JB
Dec 14 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.