declare and return struct in one line

M

Matt Graham

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
 
J

John Harrison

Matt Graham said:
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
 
V

Victor Bazarov

Matt Graham said:
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
 
V

Victor Bazarov

John Harrison said:
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
 
V

Victor Bazarov

E. Robert Tisdale said:
Victor said:

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
 
S

Sam Holden

E. Robert Tisdale said:
Victor said:
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).
 
M

Matt Graham

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

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
 
V

Victor Bazarov

Matt Graham said:
"Victor Bazarov" <[email protected]> wrote in message

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;
};
What is the correct term? line? statement?
I'm referring to cout << "statement?";
in the program

"Statement" is appropriate, IMO.

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



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

Yes, it should.

Victor
 
M

Matt Graham

Victor Bazarov said:
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;
};

I use both C and C++ and hopefully won't catch hell for saying so in
this group, but I never really worry too much about which syntax I
use. Does it matter much to the compiler? other than readability and
consistency to the user?

Also I had meant to mention that it's a system defined type and the C
syntax is for compatibility.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top