Thread: Few simple questions...

  1. #1
    Learning the art of C++
    Join Date
    Aug 2005
    Posts
    7

    Question Few simple questions...

    I'll get to the point. What is the advantage of using a struct instead of a class? The only difference is that classes can have private variables....correct?

    Also, I'm sort of confused as to how pointers work. I know how the "&" reference symbol works. For example...

    Code:
    int var = 1;
    var* = 2;
    Do var and *var have two seperate values? Like two completely seperate variables?....I'm confused.

  2. #2
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    I'll also get to the point. The only difference between a struct and class is that members of a struct are public by default while members of a class are private by default.

    Your pointer sample doesn't make sense. It'd do you good to search the net for a good pointer tutorial. Speaking of which, I think I'll write one up and put it on my site sometimes soon.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by Shane
    I'll get to the point. What is the advantage of using a struct instead of a class? The only difference is that classes can have private variables....correct?
    Lucky already posted the differences between a struct and a class but to just clarify things a tiny bit more a struct can also have private variables and member functions:

    Code:
    struct my_struct
    {
        int foo;           // Public (by default) data member
        void func1(void);  // Public (by default) member function
        my_struct(void);   // Public constructor
    private:
        int bar;           // Private data member
        void func2(void);  // Private member function
    };
    
    my_struct::my_struct(void)
    {
        ...
    }
    
    void my_struct::func1(void)
    {
        ...
    }
    
    void my_struct::func2(void)
    {
        ...
    }
    Your pointer example needs a little work, try this for starters:
    Code:
    // Create integer variable "var" that stores the value 1
    int var = 1;
    // Create integer pointer variable "var_ptr" storing the address of the variable "var"
    int * var_ptr = &var;
    
    // Output address of "var" and the value of "var" using the pointer variable "var_ptr"
    cout << "Address of var is: " << (int)var_ptr
         << " and the value stored there is: " << *var_ptr << endl;
    Last edited by hk_mp5kpdw; 08-04-2005 at 06:09 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Even though classes and structs are virtually interchangable in C++, people often use structs to group together public data without any interface functions, and use classes for everything else, which usually means everything that keeps its member data private and provides public interface methods to access that data.

  5. #5
    Amazingly beautiful user.
    Join Date
    Jul 2005
    Location
    If you knew I'd have to kill you
    Posts
    254
    As far as pointers, someone pointed out a great Video Tutorial earlier. It explains the concept perfectly, and is hilarious. Definately worht a watch.
    http://cslibrary.stanford.edu/104/
    Programming Your Mom. http://www.dandongs.com/

  6. #6
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    I don't think there is any "advantage" to structures. C had structures (but not classes). So structures couldn't be left-out of C++. In C, they could hold only data, they could not have member functions.

    So like Daved suggested, I only use structures to only hold data... structures that would be valid in C. Whenever I need a member function, I'll use a class.


    To me, the syntax for pointers and references has always seemed screwy, especially the '*' which means "this is a pointer" when you define the pointer, but everywhere else it means "dereference" (go get the value from wherever this points). You just have to accept and learn the syntax.

    The '&' symbol generally means "address of".

    A pointer is a variable. The value of that variable (the number the variable holds) represents an address of another variable.

    A reference is another variable that exists at the same exact memory address as the associated original variable. It's really the same variable with another name.

    With all of this "address" stuff, you rarely need to know or directly use the actual addresses. The compiler keeps track of them behind the scenes.
    Last edited by DougDbug; 08-04-2005 at 07:41 PM.

  7. #7
    Learning the art of C++
    Join Date
    Aug 2005
    Posts
    7
    Thanks everyone for the replies....I really do appreciate it and am beginning to grasp pointers a bit better.

    Pointers, for some strange reason, have always been the hardest thing about c++ for me to learn.

  8. #8
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Check out the "What's Up With Pointers?" tutorial here.

    I hope it helps. Let me know.

  9. #9
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    Lol very nice lucky, I like the liberal use of illustrations when appropriate, and of course the use of the word mofo for describing a pointer .

    I think its a good tutorial showing how pointers can eventually be used in bigger and better things.

  10. #10
    Learning the art of C++
    Join Date
    Aug 2005
    Posts
    7
    That was an awesome tutorial lucky....best one on pointers I have read yet. It's not just a concept resting on the edge of my brain now....I think I mostly understand it at this point, I really appreciate everyone's input. And, of course, I will continue to study more and learn.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Two simple questions.
    By Queatrix in forum Windows Programming
    Replies: 10
    Last Post: 10-09-2005, 05:10 PM
  3. A couple questions
    By Flakster in forum C++ Programming
    Replies: 7
    Last Post: 08-09-2005, 01:22 PM
  4. A few simple batch questions
    By sean in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 07-02-2003, 01:35 PM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM