pointer to structure?

Discussion in 'C++' started by Firewalker, Oct 20, 2004.

  1. Firewalker

    Firewalker Guest

    Hello experts,
    I have a quick question. Can you create a pointer to structure or just to
    classes?

    Thank yu very much
    Firewalker, Oct 20, 2004
    #1
    1. Advertising

  2. Firewalker wrote in news: in
    comp.lang.c++:

    > Hello experts,
    > I have a quick question. Can you create a pointer to structure or just to
    > classes?
    >


    Yes.

    There is only *one* difference between struct's and class's and that's
    that members of class's are private by default and members of struct's
    are public by default.

    HTH.

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
    Rob Williscroft, Oct 20, 2004
    #2
    1. Advertising

  3. Firewalker

    Phlip Guest

    Firewalker wrote:

    > I have a quick question. Can you create a pointer to structure or just to
    > classes?


    The only difference between struct's and classes are class members are
    private by default. But neither classes nor structs occupy memory to point
    to. You mean "can you create a pointer to a struct's object, or just to
    class objects".

    You may point to...

    - functions
    - class objects
    - struct objects
    - data primitives (int, float), etc
    - arrays (roughly)

    You may also "point" to data members and member functions, but that's really
    a "smart offset". Look "member pointers" up in the FAQ, via
    http://www.slack.net/~shiva/welcome.txt.

    --
    Phlip
    http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces
    Phlip, Oct 20, 2004
    #3
  4. Firewalker

    Firewalker Guest

    Ohh. That was so helpful. Thank you guys/
    "Phlip" <> wrote in message
    news:1Dldd.15683$...
    > Firewalker wrote:
    >
    >> I have a quick question. Can you create a pointer to structure or just to
    >> classes?

    >
    > The only difference between struct's and classes are class members are
    > private by default. But neither classes nor structs occupy memory to point
    > to. You mean "can you create a pointer to a struct's object, or just to
    > class objects".
    >
    > You may point to...
    >
    > - functions
    > - class objects
    > - struct objects
    > - data primitives (int, float), etc
    > - arrays (roughly)
    >
    > You may also "point" to data members and member functions, but that's
    > really
    > a "smart offset". Look "member pointers" up in the FAQ, via
    > http://www.slack.net/~shiva/welcome.txt.
    >
    > --
    > Phlip
    > http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces
    >
    >
    Firewalker, Oct 20, 2004
    #4
  5. Firewalker wrote:

    > Can you create a pointer to structure or just to classes?


    No.
    A C++ struct or class is a type not an object.
    You can only create pointers to objects of some type.
    E. Robert Tisdale, Oct 20, 2004
    #5
  6. Firewalker

    Sharad Kala Guest

    "Rob Williscroft" <> wrote in message
    > Firewalker wrote in news: in


    > There is only *one* difference between struct's and class's and that's


    On a side note, the class keyword is superior to struct in one more way. You
    can't write - template <struct Mytype>, but template <class Mytype> is
    perfectly legal.

    :)

    Sharad
    Sharad Kala, Oct 20, 2004
    #6
  7. Rob Williscroft wrote:
    > Yes.
    >
    > There is only *one* difference between struct's and class's and that's
    > that members of class's are private by default and members of struct's
    > are public by default.
    >


    And that

    struct A{};
    struct B: A{};

    is the same as

    struct A{};
    struct B: public A{};

    while

    class C{};
    class D: C{};

    is equivalent to

    class C{};
    class D: private C{};


    Regards,
    Jacques.
    Jacques Labuschagne, Oct 20, 2004
    #7
  8. Firewalker

    Pedro Graca Guest

    class vs struct [was Re: pointer to structure?]

    Sharad Kala wrote:
    > can't write - template <struct Mytype>, but template <class Mytype> is
    > perfectly legal.


    [newbie]
    hmmm ... I want to check if structs are compatible with classes for
    templates.

    So I wrote a small program and verified that is indeed the case :)

    Comments from you gurus appreciated.




    #include <iostream>


    /* struct */
    /* this should be in a header file */
    struct S {
    int a;
    int b;
    S operator+(S);
    };

    /* this should be in a separate code file */
    S S::eek:perator+(S x) {
    S temp;
    temp.a = this->a + x.a;
    temp.b = this->b + x.b;
    return temp;
    }

    std::eek:stream & operator<<(std::eek:stream & in, const S & x) {
    std::cout << "(" << x.a << ", " << x.b << ")";
    return in;
    }
    /* end struct */

    /* class */
    /* this should be in a header file */
    class C {
    int a;
    int b;
    public:
    C(int, int);
    C operator+(C);
    friend std::eek:stream & operator<<(std::eek:stream & in, const C & x);
    };

    /* this should be in a separate code file */
    C::C(int x, int y) {
    this->a = x;
    this->b = y;
    }

    C C::eek:perator+(C x) {
    C temp(0, 0);
    temp.a = this->a + x.a;
    temp.b = this->b + x.b;
    return temp;
    }

    std::eek:stream & operator<<(std::eek:stream & in, const C & x) {
    std::cout << "(" << x.a << ", " << x.b << ")";
    return in;
    }
    /* end class */

    template<class CLASS_OR_STRUCT>
    CLASS_OR_STRUCT test_sum(CLASS_OR_STRUCT x, CLASS_OR_STRUCT y) {
    return x + y;
    }

    int main() {
    int ix=6, iy=-5;
    double dx=-3, dy=5;
    S Sx={5, 0}, Sy={-2, 4};
    C Cx(-3, 3), Cy(8, 3);

    using std::cout;
    using std::endl;

    cout << test_sum(ix, iy) << endl;
    cout << test_sum(dx, dy) << endl;
    cout << test_sum(Sx, Sy) << endl;
    cout << test_sum(Cx, Cy) << endl;
    }


    --
    USENET would be a better place if everybody read:
    http://www.expita.com/nomime.html
    http://www.netmeister.org/news/learn2quote2.html
    http://www.catb.org/~esr/faqs/smart-questions.html
    Pedro Graca, Oct 20, 2004
    #8
  9. Firewalker

    Sharad Kala Guest

    Re: class vs struct [was Re: pointer to structure?]

    "Pedro Graca" <> wrote in message

    > hmmm ... I want to check if structs are compatible with classes for
    > templates.


    They are compatible i.e. you can have struct templates like class templates.

    > So I wrote a small program and verified that is indeed the case :)


    The point I was making was that you cannot write this in your sample
    program -
    template<struct CLASS_OR_STRUCT>
    ^^^^
    CLASS_OR_STRUCT test_sum(CLASS_OR_STRUCT x, CLASS_OR_STRUCT y) {
    return x + y;
    }

    Sharad
    Sharad Kala, Oct 20, 2004
    #9
  10. Firewalker

    Ron Natalie Guest

    Rob Williscroft wrote:

    >
    > There is only *one* difference between struct's and class's and that's
    > that members of class's are private by default and members of struct's
    > are public by default.
    >

    Yes, and to go farther... structs ARE classes in C++.
    Ron Natalie, Oct 20, 2004
    #10
  11. Firewalker

    Pedro Graca Guest

    Re: class vs struct [was Re: pointer to structure?]

    Sharad Kala wrote:
    > "Pedro Graca" <> wrote in message
    >> So I wrote a small program and verified that is indeed the case :)

    >
    > The point I was making was that you cannot write this in your sample
    > program -
    > template<struct CLASS_OR_STRUCT>
    > ^^^^



    Ah!
    To avoid confusions I'll write


    template<typename whatever[, ...]> ...


    in the future.
    Unless, of course, some one here persuades me otherwise.

    --
    USENET would be a better place if everybody read:
    http://www.expita.com/nomime.html
    http://www.netmeister.org/news/learn2quote2.html
    http://www.catb.org/~esr/faqs/smart-questions.html
    Pedro Graca, Oct 20, 2004
    #11
  12. Firewalker

    Jerry Coffin Guest

    Re: class vs struct [was Re: pointer to structure?]

    Pedro Graca <> wrote in message news:<-berlin.de>...

    [ ... ]

    > To avoid confusions I'll write
    >
    >
    > template<typename whatever[, ...]> ...
    >
    >
    > in the future.
    > Unless, of course, some one here persuades me otherwise.


    One naming system (that I first saw in _Modern C++ Design_) that seems
    fairly reasonable to me is to use 'typename' when the parameter can be
    any type, and 'class' when it is restricted to a UDF.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
    Jerry Coffin, Oct 21, 2004
    #12
    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. Replies:
    10
    Views:
    695
    Chris Torek
    Feb 4, 2005
  2. jimjim
    Replies:
    16
    Views:
    835
    Jordan Abel
    Mar 28, 2006
  3. Replies:
    6
    Views:
    371
    Jack Klein
    Aug 27, 2006
  4. Replies:
    4
    Views:
    1,248
    Fred Zwarts
    Jul 2, 2009
  5. A
    Replies:
    27
    Views:
    1,592
    Jorgen Grahn
    Apr 17, 2011
Loading...

Share This Page