return value

C

Carmen Sei

Is there any way to return either NULL or a SYSTEMTIME object? will
it work in C++?

as in Java, one can return either NULL or an object, does C++ has
something similar?

Is the following code OK for C++?
=================

SYSTEMTIME Table::Get(char* FieldName)
{
_variant_t vtValue;
vtValue = m_Rec->Fields->GetItem(FieldName)->GetValue();

if (vtValue.vt == VT_NULL) {
return NULL;
}
SYSTEMTIME m_st;
VariantTimeToSystemTimeWithMilliseconds (vtValue.date, &m_st);
return m_st;
}
 
I

Ian Collins

[why are you posting similar questions under two aliases?]

Carmen said:
Is there any way to return either NULL or a SYSTEMTIME object? will
it work in C++?
What's a SYSTEMTIME object? Looks like a macro name to me.

Can't you define an error value for one?
 
C

Carmen Sei

typedef struct _SYSTEMTIME {
WORD wYear;
WORD wMonth;
WORD wDayOfWeek;
WORD wDay;
WORD wHour;
WORD wMinute;
WORD wSecond;
WORD wMilliseconds;
} SYSTEMTIME, *PSYSTEMTIME, *LPSYSTEMTIME;


what do you mean by " define an error value for one"?

so my goal is a function get a "FieldName" as parameter, then it will
return NULL is "FieldName" not found, or return the SYSTEMTIME object
if found.
 
I

Ian Collins

[don't top post or snip attributions]

Carmen said:
what do you mean by " define an error value for one"?
An invalid value for that function to return.
 
J

James Kanze

What's a SYSTEMTIME object? Looks like a macro name to me.
Can't you define an error value for one?

Typically, you probably can't. Especially if, as I suspect
here, the type was defined elsewhere. In practice, singular
error values work for pointers (where the standard has defined
the value null) and in special cases, for integers (function
returns a small non-negative integer, or -1 for error).

If the object is copiable, and has a default constructor, you
can use fallible; this is pretty much standard procedure in my
code. (It does mean sometimes providing a default constructor
which wouldn't otherwise make sense. It's on my TODO list to
eliminate the default constructor requirement. But in practice,
it's never been a real problem in my code, so eliminating it
doesn't have very high priority.)
 
I

Ian Collins

James said:
Typically, you probably can't. Especially if, as I suspect
here, the type was defined elsewhere. In practice, singular
error values work for pointers (where the standard has defined
the value null) and in special cases, for integers (function
returns a small non-negative integer, or -1 for error).

In this case, probably yes. Just set one field of the struct to an out
of range value, line -1.
 
J

James Kanze

In this case, probably yes. Just set one field of the struct
to an out of range value, line -1.

Sounds like a horrible hack to me? Which one? And why that
one, and not some other one? Or maybe we should require all of
them to be -1?
 
N

Nick Keighley

A NULL pointer in C++ means return 0 ??

you posted your reply before what you are repltying to. This is
called
top posting. It rapidly leads to a very confused post.

You also removed the attributions. Please leave them in.

You messed up the attributions so that "so you" appeared to be
posted by you. I have corrected that.

In C++ NULL is a maro that expands to a null pointer constant.
A common value is 0. In a pointer context a 0 (zero) in
the source text indicates a null pointer context. So if you
want a function to return a null pointer constant return 0
or NULL. For hostorical reasons 0 is often preferred to NULL
in C++.

For more details about NULL and null pointer constants see
the comp.lang.c FAQ (I'm not sure if the C++ FAQ goes into
as much detail).
 

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
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top