#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2002
    Location
    BCN
    Posts
    84
    Rep Power
    13

    Pointers or Proper members for GUI progs


    Hi,

    I'm quite new to GUI programming and I have asked myself many times a question. Why do they use pointers as members and not normal objects. I've seen tutorials for Qt and wxWindows and in both cases many times I see things like this:

    class A : public guiA
    {
    private:
    aWidget *member1;
    bWidget *menber2;
    };

    And in the implementation.
    A::A(whatever){
    member1 = new aWidget(whatever);
    member2 = new bWidget(whatever);
    }

    Why do they use this and not:
    class A : public guiA
    {
    private:
    aWidget member1;
    bWidget menber2;
    };

    And in the implementation.
    A::A(whatever){
    member1 = aWidget(whatever);
    member2 = bWidget(whatever);
    }

    Is it obvious or subtle? I can not see a big difference.
    TIA
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    Because you can't call a constructor with arguments inside the class definition. Don't forget to delete your memory in your destructor!

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2002
    Location
    BCN
    Posts
    84
    Rep Power
    13
    Ok, that I understand. But, could I do this with the two step process, aWidget member = member(); in the class definition, and then use some kind of member.create() fucntion in the implementation of the constructor?
  6. #4
  7. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    I am far from a C++ guru, all I can say is it won't compile if you try to initialize a variable in the class definition. Give it a try!

    The constructor, by the way, is not called directly, it is 'called' by the compiler, same with the destructor. The only time you have any control over when the constructor is called is when you initialize the variable.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2002
    Location
    BCN
    Posts
    84
    Rep Power
    13

    Continue


    Ok, now it's clear.

    In the class definition:
    class blabla{
    aWidget member;
    };
    In the class constructor
    blabla::blabla(parameters)
    {
    member.create(); //Or corresponding setters.
    }

    So, you can only use this approach if you can set all the necesasry properties of your widget using functions different from the constructor. Think that I suppose you can. Is it rigtht now?
  10. #6
  11. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244

    Don't blow your stack!


    One of the things to keep in mind is that your stack is only so large. Everything instantiated in a class (like your example) gets allocated on the stack. Classes can very quickly bloat to enormous sizes and simply declaring one can cause your program to crash. It has happened to me and the feedback you get from the debugger is very non-intuitive. I think that is why most objects are declared as pointers and instantiated with a "new" so the get created on the heap. A bit more expensive and requires explicit deletes, but more or less guarenteed not to cause a stack blowout.

    This is likely the reason why Windows MFC does everything with pointers.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    Why do they use pointers as members and not normal objects?

    If you know the difference between passing by value and passing by reference to a function, then you should understand why pointers are necessary a lot of times. If you don't understand that, then you should learn about it, and until then the other main reason is: efficiency, i.e. programs run faster.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2002
    Location
    BCN
    Posts
    84
    Rep Power
    13
    Ok, I haven't thought about the stack-heap problem. That makes lots of sense. This may be the real reason why they do that with pointers. Thanks a lot for all the interesting remarks.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2002
    Location
    BCN
    Posts
    84
    Rep Power
    13
    Hi again,

    Another question. I've seen some times in the constructors things like the following:

    aWidget *dum = new aWidget(bla,bla)

    Even if it works, is, as it seems to me, a memory leak this?
    TIA
  18. #10
  19. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    >> Even if it works, is, as it seems to me, a memory leak this?
    Not if you have a corresponding
    delete dum;
    statement after you're done with dum. Of course, this is assuming that aWidget is written correctly and deallocates anything that it may have allocated. :D
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  20. #11
  21. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    You will typically see something like this:

    Code:
    class dum{
    private:
        DUMBER *dumber;
    
    public:
        dum(){
            //create object on heap at construction time
            dumber = new DUMBER(bla, bla);
        }
            
        ~dum(){
            //destroy heap object in destructor, compiler ensures
            //there is no memory leak
            delete dumber;
        }
    }

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

IMN logo majestic logo threadwatch logo seochat tools logo