Thread: Pointers

  1. #16
    Registered User
    Join Date
    Sep 2004
    Posts
    40
    my understanding is clearing up but if you want to add to this it would be appreciated. simple question, are structures the same as classes. can one be substituted for the other and is there a peticular use for one that the other can't do.
    Last edited by pizzapie; 09-27-2004 at 03:18 AM.

  2. #17
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Ah i see never really worked with void pointers or really pointers that much (ussally use references ) before thats why I added the almighty get out of getting made fun of "I believe"
    Woop?

  3. #18
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    structs in C++ are the same as classes BUT you ussally only want to use a struct if you only need data only no member functions.
    Last edited by prog-bman; 09-27-2004 at 03:27 AM. Reason: I can't spell
    Woop?

  4. #19
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    pointers can do some pretty powerful things which you will eventually learn.... for now you can probably just think of them as being a way to optimize your code.

    say you pass a variable to a function...each time you call that function the program first has to create space for the variable, then copy the contents of the variable into the newly allocated space. however if you pass by reference (use a pointer), the function can just go straight to the address in memory and access the data rather than going through the overhead of creating a whole new variable. it may not sound like much, but numerous functions calls with large data structures can slow down a program alot.

    pointers are good for a lot more, but you won't really learn to appreciate them until you see what they can do. search the web for "pointers to a function" or skip ahead in your book (or whatever) and look at linked lists. actually yea, look up linked lists. i think that's the best way to learn and understand pointers.


    ok, i'm done now...beer makes me type alot

  5. #20
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Whether structs or classes are the same is not really a question for this thread, but I'll answer it anyway.

    In C++, structs are realised as classes, but with public visibility and public inheritance by default. So they are interchangeable. However, it is a good idea to make all structs have only data members, perhaps with a constructor that properly initializes all members.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #21
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    oh damn.......i didn't realize there was a second page or i wouldn't have post the previous

    you can do the same thing with structs as you can with classes until you get into inheritance and polymorphism... but you only want to use structs to store data.....use classes to store AND process data.

  7. #22
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    Arent structs and classes stored in memory differently: I remember reading somewhere that classes are stored in the heap where structs go on the stack?
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  8. #23
    Registered User
    Join Date
    Sep 2004
    Posts
    40
    thats cool. now one last request before i jump off a cliff. give me a link to a tutorial that covers classes perfectly, describes everything about them etc. must be recent

  9. #24
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    *grumbles something incoherent about people posting what they don't know about*

    Sorry. I'm really sorry.

    But:
    you can do the same thing with structs as you can with classes until you get into inheritance and polymorphism
    WRONG. You can do exactly the same things, even the complicated ones. You just don't.
    Arent structs and classes stored in memory differently: I remember reading somewhere that classes are stored in the heap where structs go on the stack?
    WRONG. You may have read this about C#, where it is indeed so. In C++, something is on the stack if you declare it as a local variable and on the heap if you allocate it with new.

    pizzapie, I don't know what you mean by "perfectly", but you could of course try a few lessons from my tutorial. Not viewable in Internet Explorer.
    http://stud3.tuwien.ac.at/~e0226430/.../classes.xhtml
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #25
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    pfft...programmers.... it's always so technical and literal...... never can just take a concept and accept it as an idea. when people ask something like what's the difference between classes and structures there's no need to get all technical. just give them a concept, let them form an idea, they read/learn/test their idea and reinforce it. after they understand the concept is when you go back and tell them, "yea, technically it's the same, but..."

  11. #26
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    I was just asking a question i wasn't stating that it was a fact notice my ?. I was trying to clarify and I aprreciate that you have. I read a lot and forgot where I read that. But i wasn't wrong because I wasn't answering anything.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  12. #27
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Ah, here I am, known now as the arrogant know-it-all. I knew it would happen.

    misplaced: If you're talking about concepts, then DO talk about concepts, I have no problem with that. But the first part of your post was formulated as technical possibility, not as a concept.
    manofsteel: Unfortunately, this kind of question is all too easily taken as an answer.

    Believe me, I have experience with this kind of stuff. Whatever you say can and will be held against you.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #28
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    Forgive me. I didn't mean to mislead anyone. I take no offense and I am sure you are very familiar with this situation. Believe me I don't want to be a blind man trying to lead the blind. I know what I don't know and I know what I do know and I don't try to answer questions I don't know. However sometimes I misunderstand as some others have and there lies the problem thinking you know something when you actually don't lol. Oh maybe it is the caffeene I am rambling again. Anyway thank you for the clarification. I aprreciate you sharing your knowledge. Feel free to correct me anytime. Hopefully I will learn something.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM