Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   declare and return struct in one line (http://www.velocityreviews.com/forums/t276009-declare-and-return-struct-in-one-line.html)

Matt Graham 07-24-2003 09:51 PM

declare and return struct in one line
 
DateType GetDateElement(UInt8 ui_column) { DateType t = { 0 }; return
t; }

DateType is a structure;

Is there a way to declare the DateType, initialize it to 0s and return
it in one command? I don't think this matters, but this is a member
function in a class.

Thanks,
Matt Graham

John Harrison 07-24-2003 10:07 PM

Re: declare and return struct in one line
 

"Matt Graham" <mgraham@cymedix.com> wrote in message
news:28763143.0307240613.5cd8f757@posting.google.c om...
> DateType GetDateElement(UInt8 ui_column) { DateType t = { 0 }; return
> t; }
>
> DateType is a structure;
>
> Is there a way to declare the DateType, initialize it to 0s and return
> it in one command? I don't think this matters, but this is a member
> function in a class.
>
> Thanks,
> Matt Graham


Give your DateType a constructor which initialises it to 0s. Then write

DateType GetDateElement(UInt8 ui_column) { return DateType(); }

john



Victor Bazarov 07-24-2003 10:13 PM

Re: declare and return struct in one line
 
"Matt Graham" <mgraham@cymedix.com> wrote...
> DateType GetDateElement(UInt8 ui_column) { DateType t = { 0 }; return
> t; }
>
> DateType is a structure;
>
> Is there a way to declare the DateType, initialize it to 0s and return
> it in one command? I don't think this matters, but this is a member
> function in a class.


There is no such thing as "command" in C++. You need to be more
clear what you need to do.

If you want to combine the declaration/definition/initialisation
which you have in the form

DataType t = { 0 };

with the following return statement, then _usually_ it is possible
to do by writing

return DataType();

Depending on what 'DataType' is, of course. Without seeing the
definition of the 'DataType' type it's difficult to be sure.

One cannot _define_ the DataType _type_ in the same statement.

If those guesses do not answer your question, try to rephrase it.

Victor



Victor Bazarov 07-24-2003 10:19 PM

Re: declare and return struct in one line
 
"John Harrison" <john_andronicus@hotmail.com> wrote...
>
> "Matt Graham" <mgraham@cymedix.com> wrote in message
> news:28763143.0307240613.5cd8f757@posting.google.c om...
> > DateType GetDateElement(UInt8 ui_column) { DateType t = { 0 }; return
> > t; }
> >
> > DateType is a structure;
> >
> > Is there a way to declare the DateType, initialize it to 0s and return
> > it in one command? I don't think this matters, but this is a member
> > function in a class.
> >
> > Thanks,
> > Matt Graham

>
> Give your DateType a constructor which initialises it to 0s. Then write
>
> DateType GetDateElement(UInt8 ui_column) { return DateType(); }


If 'DateType' is a POD, shouldn't it already do that?

Victor



E. Robert Tisdale 07-24-2003 10:47 PM

Re: declare and return struct in one line
 
Victor Bazarov wrote:

> There is no such thing as "command" in C++.


Is there such a thing as an "imperative" in C++?

I looked up imperative in the Americal Heritage Dictionary

http://www.bartleby.com/61/

NOUN:1a. A command; an order.


E. Robert Tisdale 07-25-2003 12:34 AM

Re: declare and return struct in one line
 
Victor Bazarov wrote:

> E. Robert Tisdale wrote:
>
>>Victor Bazarov wrote:
>>
>>
>>>There is no such thing as "command" in C++.

>>
>>Is there such a thing as an "imperative" in C++?

>
>
> No.


Is C++ and imperative [computer] programming language or not?


Victor Bazarov 07-25-2003 02:34 AM

Re: declare and return struct in one line
 
"E. Robert Tisdale" <E.Robert.Tisdale@jpl.nasa.gov> wrote...
> Victor Bazarov wrote:
>
> > E. Robert Tisdale wrote:
> >
> >>Victor Bazarov wrote:
> >>
> >>
> >>>There is no such thing as "command" in C++.
> >>
> >>Is there such a thing as an "imperative" in C++?

> >
> >
> > No.

>
> Is C++ and imperative [computer] programming language or not?


The Standard defines C++ as "a general purpose programming
language". There is not one occurrence of "imperative" in the
C++ Standard.

Victor



Sam Holden 07-25-2003 03:35 AM

Re: declare and return struct in one line
 
On Fri, 25 Jul 2003 02:34:02 GMT, Victor Bazarov <v.Abazarov@attAbi.com> wrote:
> "E. Robert Tisdale" <E.Robert.Tisdale@jpl.nasa.gov> wrote...
>> Victor Bazarov wrote:
>>
>> > E. Robert Tisdale wrote:
>> >
>> >>Victor Bazarov wrote:
>> >>
>> >>
>> >>>There is no such thing as "command" in C++.
>> >>
>> >>Is there such a thing as an "imperative" in C++?
>> >
>> >
>> > No.

>>
>> Is C++ and imperative [computer] programming language or not?

>
> The Standard defines C++ as "a general purpose programming
> language". There is not one occurrence of "imperative" in the
> C++ Standard.


So? What benefit would there be in the standard declaring the obvious
type of the language?

The C++ model of computation is of a program state and statements which
change that state. In other words the imperative programming model.

As opposed to functional languages (computation is the evaluation of
math functions) or declarative languages (computation is the satisfaction of
specified properties of a solution).

--
Sam Holden


Matt Graham 07-25-2003 01:50 PM

Re: declare and return struct in one line
 
"Victor Bazarov" <v.Abazarov@attAbi.com> wrote in message news:<vi0mh773gkjtbe@corp.supernews.com>...
> "Matt Graham" <mgraham@cymedix.com> wrote...
> > DateType GetDateElement(UInt8 ui_column) { DateType t = { 0 }; return
> > t; }
> >
> > DateType is a structure;


typedef struct {
UInt16 year :7;
UInt16 month :4;
UInt16 day :5;
} DateType;


> > Is there a way to declare the DateType, initialize it to 0s and return
> > it in one command? I don't think this matters, but this is a member
> > function in a class.

>
> There is no such thing as "command" in C++. You need to be more
> clear what you need to do.


What is the correct term? line? statement?
I'm referring to cout << "statement?";
in the program

main() {
cout << "statement?";
return 0;
}


> If you want to combine the declaration/definition/initialisation
> which you have in the form
>
> DataType t = { 0 };
>
> with the following return statement, then _usually_ it is possible
> to do by writing
>
> return DataType();


This compiles ok. And it automatically defaults a POD structure to 0?
That's pretty cool

Victor Bazarov 07-25-2003 02:02 PM

Re: declare and return struct in one line
 
"Matt Graham" <mgraham@cymedix.com> wrote...
> "Victor Bazarov" <v.Abazarov@attAbi.com> wrote in message

news:<vi0mh773gkjtbe@corp.supernews.com>...
> > "Matt Graham" <mgraham@cymedix.com> wrote...
> > > DateType GetDateElement(UInt8 ui_column) { DateType t = { 0 }; return
> > > t; }
> > >
> > > DateType is a structure;

>
> typedef struct {
> UInt16 year :7;
> UInt16 month :4;
> UInt16 day :5;
> } DateType;


How long has it been since you stopped using C as your main
language? In C++ it is better to get used to declaring types
this way:

struct DateType {
UInt16 year :7;
UInt16 month :4;
UInt16 day :5;
};

> > > Is there a way to declare the DateType, initialize it to 0s and return
> > > it in one command? I don't think this matters, but this is a member
> > > function in a class.

> >
> > There is no such thing as "command" in C++. You need to be more
> > clear what you need to do.

>
> What is the correct term? line? statement?
> I'm referring to cout << "statement?";
> in the program


"Statement" is appropriate, IMO.

>
> main() {


int main() {

> cout << "statement?";
> return 0;
> }
>
>
> > If you want to combine the declaration/definition/initialisation
> > which you have in the form
> >
> > DataType t = { 0 };
> >
> > with the following return statement, then _usually_ it is possible
> > to do by writing
> >
> > return DataType();

>
> This compiles ok. And it automatically defaults a POD structure to 0?
> That's pretty cool


Yes, it should.

Victor




All times are GMT. The time now is 06:42 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.