access to structure members

Discussion in 'C++' started by noone@all.com, May 31, 2007.

  1. Guest

    What is the syntax to access members of a structure without
    explicitly naming the structure in every access?

    struct mytype {
    int a;
    char* b;
    long c;
    } IT;

    How can I access the structure members in a way similar to the old pascal
    USING statement?

    using IT (
    a=5;
    b="hello";
    c=45l;
    );


    I cannot remember the c++ syntax to accomplish this.
     
    , May 31, 2007
    #1
    1. Advertising

  2. red floyd Guest

    wrote:
    > What is the syntax to access members of a structure without
    > explicitly naming the structure in every access?
    >
    > struct mytype {
    > int a;
    > char* b;
    > long c;
    > } IT;
    >
    > How can I access the structure members in a way similar to the old pascal
    > USING statement?
    >
    > using IT (
    > a=5;
    > b="hello";
    > c=45l;
    > );
    >
    >


    1. The old Pascal construct was a "with" statement.
    2. There is no way to do what you want. Why do you want to do so? Your
    code is more maintainable without it.
     
    red floyd, May 31, 2007
    #2
    1. Advertising

  3. wrote:
    > What is the syntax to access members of a structure without
    > explicitly naming the structure in every access?
    >
    > struct mytype {
    > int a;
    > char* b;
    > long c;
    > } IT;
    >
    > How can I access the structure members in a way similar to the old
    > pascal USING statement?
    >
    > using IT (
    > a=5;
    > b="hello";
    > c=45l;
    > );
    >
    >
    > I cannot remember the c++ syntax to accomplish this.


    Nobody can. There is no such syntax. Non-static data members
    require an instance of the class to be accessed. You can only
    access them without additional qualification inside a non-static
    member function, where there is an implicit "this->".

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, May 31, 2007
    #3
  4. Salt_Peter Guest

    On May 31, 11:32 am, wrote:
    > What is the syntax to access members of a structure without
    > explicitly naming the structure in every access?
    >
    > struct mytype {
    > int a;
    > char* b;
    > long c;
    >
    > } IT;
    >
    > How can I access the structure members in a way similar to the old pascal
    > USING statement?
    >
    > using IT (
    > a=5;
    > b="hello";


    Its time to unlearn the lies:
    member b is a pointer to a single character. char* b cannot store
    anything else.
    Relying on a pointer to a single char to store an array of characters
    is doomed to fail.
    The compiler has not allocated or reserved memory to store an array of
    chars.
    You'ld have to store the null-terminated character sequence using a
    buffer (ie: char b[128];).
    However - there is a better solution: std::string (its dynamic and
    packed with features).

    > c=45l;
    > );
    >
    > I cannot remember the c++ syntax to accomplish this.


    Using a function or a constructor. IT is not a type/struct/class, its
    an instance of mytype.
    Consider a primitive array: mytype arr[100];
    Which one of those 100 instances' member's did you want to modify?
    You need to tell the program which one to act on. (ie:
    array[0].set(...);)
    But wait a minute, why set anything when you can allocate AND
    initialize private members simultaneously at construction time?

    #include <iostream>
    #include <string>

    struct mytype {
    private:
    int a;
    std::string b;
    long c;
    public:
    // default ctor + init list
    mytype()
    : a(0), b("default string"), c(0) { }
    // parametized ctor + init list
    mytype(const int n,
    const std::string& s,
    const long l)
    : a(n), b(s), c(l) { }
    // friend op<< for output
    friend std::eek:stream& operator<<(std::eek:stream& os, const mytype& t)
    {
    os << "a = " << t.a;
    os << "\nb = " << t.b;
    os << "\nc = " << t.c;
    return os;
    }
    };

    int main()
    {
    mytype instance(5, "hello", 451); // done
    std::cout << instance << std::endl;

    mytype array[100];
    std::cout << array[0] << std::endl;
    }

    // Note: all 100 instances in that array[] are already set.
     
    Salt_Peter, May 31, 2007
    #4
  5. Salt_Peter Guest

    On May 31, 11:32 am, wrote:
    > What is the syntax to access members of a structure without
    > explicitly naming the structure in every access?
    >
    > struct mytype {
    > int a;
    > char* b;


    Its time to unlearn the lies:
    member b is a pointer to a single character. char* b cannot store
    anything else. Relying on a pointer to a single char to store an array
    of characters
    is doomed to fail. The compiler has not allocated or reserved memory
    to store an array of
    chars. You'ld have to store the null-terminated character sequence
    using a
    buffer (ie: char b[128];).
    However - there is a better solution: std::string (its dynamic and
    packed with features).

    > long c;
    >
    > } IT;
    >
    > How can I access the structure members in a way similar to the old pascal
    > USING statement?
    >
    > using IT (
    > a=5;
    > b="hello";
    > c=45l;
    > );
    >
    > I cannot remember the c++ syntax to accomplish this.


    Using a function or a constructor. IT is not a type/struct/class, its
    an instance of mytype. Consider a primitive array:
    mytype arr[100];
    Which one of those 100 instances' member's did you want to modify? You
    need to tell the program which one to act on. (ie: array[0].set(...);)
    But wait a minute, why set anything when you can allocate AND
    initialize private members simultaneously at construction time?

    #include <iostream>
    #include <string>

    struct mytype {
    private:
    int a;
    std::string b;
    long c;
    public:
    // default ctor + init list
    mytype()
    : a(0), b("default string"), c(0) { }
    // parametized ctor + init list
    mytype(const int n,
    const std::string& s,
    const long l)
    : a(n), b(s), c(l) { }
    // friend op<< for output
    friend std::eek:stream& operator<<(std::eek:stream& os, const mytype& t)
    {
    os << "a = " << t.a;
    os << "\nb = " << t.b;
    os << "\nc = " << t.c;
    return os;
    }

    };

    int main()
    {
    mytype instance(5, "hello", 451); // done
    std::cout << instance << std::endl;

    mytype array[100];
    std::cout << array[0] << std::endl;

    }

    // Note: all 100 instances in that array[] are already set.
     
    Salt_Peter, May 31, 2007
    #5
  6. Devon Null Guest

    wrote:
    > What is the syntax to access members of a structure without
    > explicitly naming the structure in every access?
    >
    > struct mytype {
    > int a;
    > char* b;
    > long c;
    > } IT;
    >
    > How can I access the structure members in a way similar to the old pascal
    > USING statement?
    >
    > using IT (
    > a=5;
    > b="hello";
    > c=45l;
    > );
    >
    >
    > I cannot remember the c++ syntax to accomplish this.
    >
    >

    couldn't you use #define and rename each one to an alias? Not saying it
    is a good way, just a possibility (I think). Feel free to correct me if
    I am wrong.
     
    Devon Null, May 31, 2007
    #6
  7. Pete Becker Guest

    Salt_Peter wrote:
    >> b="hello";

    >
    > Its time to unlearn the lies:
    > member b is a pointer to a single character. char* b cannot store
    > anything else.
    > Relying on a pointer to a single char to store an array of characters
    > is doomed to fail.
    > The compiler has not allocated or reserved memory to store an array of
    > chars.


    On the contrary: the compiler allocated and reserved memory for the
    string "hello", including its terminating null character. It's perfectly
    appropriate to refer to that array through a char* (although a const
    char* would be preferable). Pointers often point to a single object, but
    they also often point to the first element of an array.

    > You'ld have to store the null-terminated character sequence using a
    > buffer (ie: char b[128];).
    > However - there is a better solution: std::string (its dynamic and
    > packed with features).
    >


    That depends on what the problem is. There's nothing in the original
    question that supports a judgment about the relative merits of char*
    versus std::string. That's not important, though, since it has nothing
    to do with what was actually asked.

    --

    -- Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com)
    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." (www.petebecker.com/tr1book)
     
    Pete Becker, May 31, 2007
    #7
    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. JFCM
    Replies:
    4
    Views:
    5,753
  2. CoolPint
    Replies:
    8
    Views:
    1,000
    Jeff Schwab
    Dec 14, 2003
  3. Dave
    Replies:
    3
    Views:
    371
    tom_usenet
    Aug 10, 2004
  4. hdixon
    Replies:
    3
    Views:
    653
    hdixon
    Jul 9, 2006
  5. lovecreatesbeauty
    Replies:
    43
    Views:
    1,307
    Keith Thompson
    Feb 6, 2006
Loading...

Share This Page