Creating an instance of a class

Discussion in 'C++' started by jimbo, Feb 19, 2007.

  1. jimbo

    jimbo Guest

    Dear all,

    I am more or less new to c++ programing and therefore still have
    problems with some fundamentals :(

    At the moment I try to build a GUI-Application with Qt4. Sometimes I
    have seen in the tutorials I have to instance a class with the new
    command. Like QWidget QLabel *label = new QLabel("Hello World"); Then
    I have created a pointer to a QLabel Object. The other way I have seen
    is to just use QLabel label("Hello World"); Without a pointer and
    without the new statement.

    So to access the functions of the QLabel class I have to use the "."
    for the part without pointer like label.show and for the pointer part:
    label->show.

    Can somebody explain me, why it is possible to use the two ways and
    what is the difference in general.

    Thank you a lot in advance.

    jimbo
     
    jimbo, Feb 19, 2007
    #1
    1. Advertising

  2. On Feb 19, 9:16 am, "jimbo" <> wrote:
    > Dear all,
    >
    > I am more or less new to c++ programing and therefore still have
    > problems with some fundamentals :(
    >
    > At the moment I try to build a GUI-Application with Qt4. Sometimes I
    > have seen in the tutorials I have to instance a class with the new
    > command. Like QWidget QLabel *label = new QLabel("Hello World"); Then
    > I have created a pointer to a QLabel Object. The other way I have seen
    > is to just use QLabel label("Hello World"); Without a pointer and
    > without the new statement.
    >
    > So to access the functions of the QLabel class I have to use the "."
    > for the part without pointer like label.show and for the pointer part:
    > label->show.
    >
    > Can somebody explain me, why it is possible to use the two ways and
    > what is the difference in general.


    You really should get yourself a good book if you haven't got one
    already because it can take some time to learn those things and there
    are a lot of things to keep in mind which I can't explain in one post
    (but you are always welcome to ask questions).

    The difference between using 'QLabel label;' and QLabel* label = new
    QLabel();' is where the instance is stored and its scope. When not
    using new the instance will be created on the stack and will be
    destructed when it goes out of scope (this usually coincide with a
    '}').

    When using new the object is created on the heap and will remain there
    until you call delete on a pointer pointing to it. This means that an
    object instantiated in one function using new can be used in another
    function (even if the first function has returned) if given a pointer
    to it.

    For every time you create an object with new you must (for each and
    every path of execution) call delete on a pointer to that object or
    your object will leak memory. To make things a bit more trickier this
    is not always true when using Qt since a parent object will usually
    delete/destruct all child objects on destruction.

    An advice regarding dynamically allocated memory (using new) is to not
    use it unless you have to.

    --
    Erik Wikström
     
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Feb 19, 2007
    #2
    1. Advertising

  3. jimbo

    Piyo Guest

    jimbo wrote:
    > Dear all,
    >
    > I am more or less new to c++ programing and therefore still have
    > problems with some fundamentals :(
    >
    > At the moment I try to build a GUI-Application with Qt4. Sometimes I
    > have seen in the tutorials I have to instance a class with the new
    > command. Like QWidget QLabel *label = new QLabel("Hello World"); Then
    > I have created a pointer to a QLabel Object. The other way I have seen
    > is to just use QLabel label("Hello World"); Without a pointer and
    > without the new statement.
    >
    > So to access the functions of the QLabel class I have to use the "."
    > for the part without pointer like label.show and for the pointer part:
    > label->show.
    >
    > Can somebody explain me, why it is possible to use the two ways and
    > what is the difference in general.
    >
    > Thank you a lot in advance.
    >
    > jimbo
    >

    Given an object and a pointer to that object, you can access members
    like so:

    class foo
    {
    public:
    int bar;
    };

    int
    main()
    {
    foo* baz1 = new foo();
    foo baz2;

    baz2.bar; // cool
    baz2->bar; // not cool
    baz1.bar; // not cool
    (*baz1).bar; // cool but too long
    (baz1)->bar; // short hand version of the previous line
    baz1->bar; // even shorter version of the previous line
    }

    In summary, "->" is a short hand notation to dereference the
    pointer first and then access its member.

    Good Luck!
     
    Piyo, Feb 19, 2007
    #3
  4. jimbo

    jimbo Guest

    On 19 Feb., 09:55, Piyo <> wrote:
    > jimbo wrote:
    > > Dear all,

    >
    > > I am more or less new to c++ programing and therefore still have
    > > problems with some fundamentals :(

    >
    > > At the moment I try to build a GUI-Application with Qt4. Sometimes I
    > > have seen in the tutorials I have to instance a class with the new
    > > command. Like QWidget QLabel *label = new QLabel("Hello World"); Then
    > > I have created a pointer to a QLabel Object. The other way I have seen
    > > is to just use QLabel label("Hello World"); Without a pointer and
    > > without the new statement.

    >
    > > So to access the functions of the QLabel class I have to use the "."
    > > for the part without pointer like label.show and for the pointer part:
    > > label->show.

    >
    > > Can somebody explain me, why it is possible to use the two ways and
    > > what is the difference in general.

    >
    > > Thank you a lot in advance.

    >
    > > jimbo

    >
    > Given an object and a pointer to that object, you can access members
    > like so:
    >
    > class foo
    > {
    > public:
    > int bar;
    >
    > };
    >
    > int
    > main()
    > {
    > foo* baz1 = new foo();
    > foo baz2;
    >
    > baz2.bar; // cool
    > baz2->bar; // not cool
    > baz1.bar; // not cool
    > (*baz1).bar; // cool but too long
    > (baz1)->bar; // short hand version of the previous line
    > baz1->bar; // even shorter version of the previous line
    >
    > }
    >
    > In summary, "->" is a short hand notation to dereference the
    > pointer first and then access its member.
    >
    > Good Luck!



    Hi to all of you,

    first of all, thanks a lot for the explanation. I didn´t know that
    this whole topic is related to where the memory is allocated. Now with
    heap and stack I can imagine a little bit better what I am doing :)

    But to come back to my own little example. When i am already in a
    main() function of my app and i create the instances there without the
    new operator then i will have no problems concerning the use of the
    instance in a different scope because i am already in main and main is
    not ended yet, or?

    I think i need to take it more carefully when i implement an own class
    and use functions there.

    Thx for your help.

    Best regards,

    jimbo
     
    jimbo, Feb 19, 2007
    #4
  5. jimbo

    Dizzy Guest

    jimbo wrote:

    > But to come back to my own little example. When i am already in a
    > main() function of my app and i create the instances there without the
    > new operator then i will have no problems concerning the use of the
    > instance in a different scope because i am already in main and main is
    > not ended yet, or?


    After main ends the destructors of "static" storage objects will run. Make
    sure that none of those will reference objects from main's stack.

    > I think i need to take it more carefully when i implement an own class
    > and use functions there.


    Yeah, I think too many examples of Qt's documentation use dynamic memory
    allocation when they could use "auto" memory allocation just fine. Not to
    mention they have some strange system of keeping track of dynamic memory
    objects themselves (if derived from QObject I think) which tends to lead to
    a coding style where one forgets to use delete (it may be OK with QObject
    derived objects but certainly it's not a good coding style to apply outside
    of Qt).

    --
    Dizzy
    http://dizzy.roedu.net
     
    Dizzy, Feb 19, 2007
    #5
  6. On Feb 19, 10:06 am, "jimbo" <> wrote:
    > On 19 Feb., 09:55, Piyo <> wrote:
    >
    >
    >
    > > jimbo wrote:
    > > > Dear all,

    >
    > > > I am more or less new to c++ programing and therefore still have
    > > > problems with some fundamentals :(

    >
    > > > At the moment I try to build a GUI-Application with Qt4. Sometimes I
    > > > have seen in the tutorials I have to instance a class with the new
    > > > command. Like QWidget QLabel *label = new QLabel("Hello World"); Then
    > > > I have created a pointer to a QLabel Object. The other way I have seen
    > > > is to just use QLabel label("Hello World"); Without a pointer and
    > > > without the new statement.

    >
    > > > So to access the functions of the QLabel class I have to use the "."
    > > > for the part without pointer like label.show and for the pointer part:
    > > > label->show.

    >
    > > > Can somebody explain me, why it is possible to use the two ways and
    > > > what is the difference in general.

    >
    > > > Thank you a lot in advance.

    >
    > > > jimbo

    >
    > > Given an object and a pointer to that object, you can access members
    > > like so:

    >
    > > class foo
    > > {
    > > public:
    > > int bar;

    >
    > > };

    >
    > > int
    > > main()
    > > {
    > > foo* baz1 = new foo();
    > > foo baz2;

    >
    > > baz2.bar; // cool
    > > baz2->bar; // not cool
    > > baz1.bar; // not cool
    > > (*baz1).bar; // cool but too long
    > > (baz1)->bar; // short hand version of the previous line
    > > baz1->bar; // even shorter version of the previous line

    >
    > > }

    >
    > > In summary, "->" is a short hand notation to dereference the
    > > pointer first and then access its member.

    >
    > > Good Luck!

    >
    > Hi to all of you,
    >
    > first of all, thanks a lot for the explanation. I didn´t know that
    > this whole topic is related to where the memory is allocated. Now with
    > heap and stack I can imagine a little bit better what I am doing :)
    >
    > But to come back to my own little example. When i am already in a
    > main() function of my app and i create the instances there without the
    > new operator then i will have no problems concerning the use of the
    > instance in a different scope because i am already in main and main is
    > not ended yet, or?


    Yes, the scope of main is a superset of almost all other scopes
    (static being the only exception that I can think of right now).

    > I think i need to take it more carefully when i implement an own class
    > and use functions there.


    It's not so much when implementing a class as when using it. But since
    we are on the topic of implementing classes and dynamic memory
    allocation I can say that if you implementation uses new instantiate
    members (perhaps in the constructor) you should probably delete them
    in the destructor, like so:

    class Foo {
    int* arr; // An array
    public:
    Foo(int i) : arr(new int { } // * allocate an array of size i
    ~Foo() { delete[] arr; } // Free the memory used
    };

    This way a user of the class will never have to worry about how the
    class manages its resources, it will just work.

    * If you don't understand the ': arr(new int { }'-bit it's an
    initializer-list and does the same thing as '{ arr = new int; }',
    for a more detailed explanation see http://www.parashift.com/c -faq-lite/ctors.html#faq-10.6

    --
    Erik Wikström
     
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Feb 19, 2007
    #6
  7. jimbo

    jimbo Guest

    On 19 Feb., 10:46, "Erik Wikström" <> wrote:
    > On Feb 19, 10:06 am, "jimbo" <> wrote:
    >
    >
    >
    > > On 19 Feb., 09:55, Piyo <> wrote:

    >
    > > > jimbo wrote:
    > > > > Dear all,

    >
    > > > > I am more or less new to c++ programing and therefore still have
    > > > > problems with some fundamentals :(

    >
    > > > > At the moment I try to build a GUI-Application with Qt4. Sometimes I
    > > > > have seen in the tutorials I have to instance a class with the new
    > > > > command. Like QWidget QLabel *label = new QLabel("Hello World"); Then
    > > > > I have created a pointer to a QLabel Object. The other way I have seen
    > > > > is to just use QLabel label("Hello World"); Without a pointer and
    > > > > without the new statement.

    >
    > > > > So to access the functions of the QLabel class I have to use the "."
    > > > > for the part without pointer like label.show and for the pointer part:
    > > > > label->show.

    >
    > > > > Can somebody explain me, why it is possible to use the two ways and
    > > > > what is the difference in general.

    >
    > > > > Thank you a lot in advance.

    >
    > > > > jimbo

    >
    > > > Given an object and a pointer to that object, you can access members
    > > > like so:

    >
    > > > class foo
    > > > {
    > > > public:
    > > > int bar;

    >
    > > > };

    >
    > > > int
    > > > main()
    > > > {
    > > > foo* baz1 = new foo();
    > > > foo baz2;

    >
    > > > baz2.bar; // cool
    > > > baz2->bar; // not cool
    > > > baz1.bar; // not cool
    > > > (*baz1).bar; // cool but too long
    > > > (baz1)->bar; // short hand version of the previous line
    > > > baz1->bar; // even shorter version of the previous line

    >
    > > > }

    >
    > > > In summary, "->" is a short hand notation to dereference the
    > > > pointer first and then access its member.

    >
    > > > Good Luck!

    >
    > > Hi to all of you,

    >
    > > first of all, thanks a lot for the explanation. I didn´t know that
    > > this whole topic is related to where the memory is allocated. Now with
    > > heap and stack I can imagine a little bit better what I am doing :)

    >
    > > But to come back to my own little example. When i am already in a
    > > main() function of my app and i create the instances there without the
    > > new operator then i will have no problems concerning the use of the
    > > instance in a different scope because i am already in main and main is
    > > not ended yet, or?

    >
    > Yes, the scope of main is a superset of almost all other scopes
    > (static being the only exception that I can think of right now).
    >
    > > I think i need to take it more carefully when i implement an own class
    > > and use functions there.

    >
    > It's not so much when implementing a class as when using it. But since
    > we are on the topic of implementing classes and dynamic memory
    > allocation I can say that if you implementation uses new instantiate
    > members (perhaps in the constructor) you should probably delete them
    > in the destructor, like so:
    >
    > class Foo {
    > int* arr; // An array
    > public:
    > Foo(int i) : arr(new int { } // * allocate an array of size i
    > ~Foo() { delete[] arr; } // Free the memory used
    >
    > };
    >
    > This way a user of the class will never have to worry about how the
    > class manages its resources, it will just work.
    >
    > * If you don't understand the ': arr(new int { }'-bit it's an
    > initializer-list and does the same thing as '{ arr = new int; }',
    > for a more detailed explanation seehttp://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6
    >
    > --
    > Erik Wikström


    Thx for the explanation. I added your link to my favourites...so I
    will look there the next time before I post :)

    Have a nice day.

    Best regards,

    jimbo
     
    jimbo, Feb 19, 2007
    #7
    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. Sridhar R
    Replies:
    14
    Views:
    1,454
    =?iso-8859-1?Q?Fran=E7ois?= Pinard
    Feb 10, 2004
  2. Gerry Sutton
    Replies:
    1
    Views:
    581
    Peter Otten
    Apr 16, 2005
  3. gentlestone
    Replies:
    6
    Views:
    996
    Bruno Desthuilliers
    Oct 6, 2009
  4. Martin P. Hellwig
    Replies:
    1
    Views:
    388
    Martin P. Hellwig
    Mar 26, 2010
  5. Olive
    Replies:
    4
    Views:
    256
    Olive
    Jul 6, 2012
Loading...

Share This Page