Announcement

Collapse
No announcement yet.

Pointer stuff in C++ (Kdevelop)

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Pointer stuff in C++ (Kdevelop)

    I've been trying to "integrate" C++ into myself.
    But somehow I'm having a litte difficulties to integrate that whole pointer stuff into my brain.

    Could someone explain to diff. to me concerning the following:
    "*" in combination with "->" and as in ex.2, a class with a direct name and init param, but then in combination with the "."

    "What's the difference between those two code examples ? Cause both seem to work and do the exact same thing. Must be a memory thing ? No (?)"

    eg:

    A class "House"

    House *OldHouse;
    OldHouse = new House(100);
    txtNr->setText( QVariant(OldHouse->getNr()).toString() );
    House OldHouse(100);
    txtNr->setText( QVariant(OldHouse.getNr()).toString() );
    Acer Aspire 5100<br />Kubuntu Hardy 8.04

    #2
    Re: Pointer stuff in C++ (Kdevelop)

    they do indeed do the same thing.
    but...

    in this example you're working with 2 memory areas:
    Code:
    House *OldHouse;
    OldHouse = new House(100);
    declares a "variable" called OldHouse as a pointer to a House "type".
    that is, OldHouse will not store the value of House.
    it will store its address in memory.
    in order to access the getNr() method of your object, then, you'll have to dereference your pointer.
    you do this by using the "->" notation.
    so, if you access the content of OldHouse, what you'll get is an integer number (memory address).
    if you access the content of the memory area pointed to by OldHouse, you'll get the content of the House object.

    in this example you're working with only 1 memory area:
    Code:
    House OldHouse(100);
    declares a "variable" called OldHouse as a House "type".
    access to this "variable" is "direct".
    so, in order to access the getNr() method of your object, you just reference it.
    and you do it by using the "." notation.
    so, if you access the content of OldHouse, you'll get the content of the House object.

    i'm aware it's a pretty unorthodox explanation.
    but hope it's useful.

    cheers
    gnu/linux is not windoze

    Comment


      #3
      Re: Pointer stuff in C++ (Kdevelop)

      Thx for the info jankushka and the fast reply really , but I already knew most of that stuff

      I should have asked my question in another way. What I was kinda looking for is....

      When do you use the first example and when do you use the other one.
      Since theres no real difference in output I mean, between the two of them.

      Are there some specific cases that explain the use of the first and second method.

      Otherwise....it would be obvious to use the second example. Reason: There's isn't extra memory used, by a variable that holds the pointer (or the objects memory address) to the object.
      Acer Aspire 5100<br />Kubuntu Hardy 8.04

      Comment


        #4
        Re: Pointer stuff in C++ (Kdevelop)

        Originally posted by v_override
        Are there some specific cases that explain the use of the first and second method.
        not with the example you posted that deals with 1 "House" only...no.
        but, say you'd need to deal with a number of "Houses".
        where number could be known or...unknown.
        you'd have to put them in some data structure (e.g a vector, a list, ...).
        take a look at this example from this tutorial:
        Code:
        // pointer to classes example
        #include <iostream>
        using namespace std;
        
        class CRectangle {
          int width, height;
         public:
          void set_values (int, int);
          int area (void) {return (width * height);}
        };
        
        void CRectangle::set_values (int a, int b) {
         width = a;
         height = b;
        }
        
        int main () {
         CRectangle a, *b, *c;
         CRectangle * d = new CRectangle[2];
         b= new CRectangle;
         c= &a;
         a.set_values (1,2);
         b->set_values (3,4);
         d->set_values (5,6);
         d[1].set_values (7,8);
         cout << "a area: " << a.area() << endl;
         cout << "*b area: " << b->area() << endl;
         cout << "*c area: " << c->area() << endl;
         cout << "d[0] area: " << d[0].area() << endl;
         cout << "d[1] area: " << d[1].area() << endl;
         delete&#91;] d;
         delete b;
         return 0;
        }
        yeah?
        gnu/linux is not windoze

        Comment


          #5
          Re: Pointer stuff in C++ (Kdevelop)

          Most C++ bibles dedicate at least a whole chapter to pointers and their uses, so there are no short answers (that I can think of) to your question.

          Googling for:
          c++ "why use pointers"
          or something similar should give you some answers.

          Comment


            #6
            Re: Pointer stuff in C++ (Kdevelop)

            Thx guys for the respons.

            And thx again jankushka for the example, gonna take a look at it

            EDIT:

            It's not so much "why" pointers are use Kubicle. I think that's rather obvious. To save memory.
            When you have an object....and would like another one....actually, a copy, you would need more memory, because you would need two objects of the same kind.
            When using pointers, you can just store the address of the object into a variable, here eg.: *a and *b.
            Where *a and *b both contain the address of the actual object.
            Also, when the object changes, you can access the new value of that object through *a and *b.
            When you don't use pointers....and have two objects, when changing one object, you would need to make sure your other object (the exact copy) changes too.

            I was just wondering what the difference was between those two techniques and not so much why they are implemented. But thx again for replying.
            Acer Aspire 5100<br />Kubuntu Hardy 8.04

            Comment


              #7
              Re: Pointer stuff in C++ (Kdevelop)

              Originally posted by v_override
              When do you use the first example and when do you use the other one.
              IMHO: I would say don't use pointers and dynamic memory(new) until you need them. Don't get paralyzed by pointers. Keep coding and learning the language and situations will arise where pointers will apply and you will no longer have that question.
              FKA: tanderson

              Comment

              Working...
              X