declare and return struct in one line

Discussion in 'C++' started by Matt Graham, Jul 24, 2003.

  1. Matt Graham

    Matt Graham Guest

    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
    Matt Graham, Jul 24, 2003
    #1
    1. Advertising

  2. "Matt Graham" <> wrote in message
    news:...
    > 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
    John Harrison, Jul 24, 2003
    #2
    1. Advertising

  3. "Matt Graham" <> 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, Jul 24, 2003
    #3
  4. "John Harrison" <> wrote...
    >
    > "Matt Graham" <> wrote in message
    > news:...
    > > 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
    Victor Bazarov, Jul 24, 2003
    #4
  5. 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, Jul 24, 2003
    #5
  6. 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?
    E. Robert Tisdale, Jul 25, 2003
    #6
  7. "E. Robert Tisdale" <> 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
    Victor Bazarov, Jul 25, 2003
    #7
  8. Matt Graham

    Sam Holden Guest

    On Fri, 25 Jul 2003 02:34:02 GMT, Victor Bazarov <> wrote:
    > "E. Robert Tisdale" <> 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
    Sam Holden, Jul 25, 2003
    #8
  9. Matt Graham

    Matt Graham Guest

    "Victor Bazarov" <> wrote in message news:<>...
    > "Matt Graham" <> 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
    Matt Graham, Jul 25, 2003
    #9
  10. "Matt Graham" <> wrote...
    > "Victor Bazarov" <> wrote in message

    news:<>...
    > > "Matt Graham" <> 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
    Victor Bazarov, Jul 25, 2003
    #10
  11. Matt Graham

    Matt Graham Guest

    "Victor Bazarov" <> wrote in message news:<>...
    > "Matt Graham" <> wrote...
    > > "Victor Bazarov" <> wrote in message

    > news:<>...
    > > > "Matt Graham" <> 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;
    > };


    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.
    Matt Graham, Jul 25, 2003
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. news.frontiernet.net
    Replies:
    6
    Views:
    1,118
    news.frontiernet.net
    Apr 16, 2004
  2. Chris Fogelklou
    Replies:
    36
    Views:
    1,370
    Chris Fogelklou
    Apr 20, 2004
  3. Tammy
    Replies:
    6
    Views:
    601
    Peter Nilsson
    Sep 8, 2008
  4. KOFKS
    Replies:
    5
    Views:
    1,192
  5. Merciadri Luca
    Replies:
    4
    Views:
    808
Loading...

Share This Page