Class pointer (newbie question)

This is a discussion on Class pointer (newbie question) within the C++ Programming forums, part of the General Programming Boards category; Code: QLabel *label = new QLabel ( "Hello Qt!" ); //this works QLabel label = new QLabel ( "Hello Qt!" ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    48

    Class pointer (newbie question)

    Code:
    QLabel  *label=  new  QLabel("HelloQt!"); //this works
    
    QLabel  label=  new  QLabel("HelloQt!"); //Error: Cannot convert QLabel* to QLabel
    When this is possible. I mean, what must be class declaration of QLabel so that this thing can happen.

    And how can I see class declaration of QLabel in Qt?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    2,361
    new always returns a pointer. you cannot assign a stack object from a pointer, unless it has an overloaded assignment operator that accepts a pointer as its right hand side parameter.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    48
    When should I use

    Code:
    Classname Object;
    Object.a;
    
    And,
    
    Classname *Object = new Object();
    Object->a;

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    In case 1, the object is on he stack and will be destroyed at the end of the block.
    The second one creates an object on the heap, which you have to 'delete' yourself to destroy.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    48
    I mean when the second case is useful?

    From what I know that stack size is assigned at compile time and heap is allocated in runtime. Hence using stack increases exe size. Is this the primary reason or there are any other?
    Last edited by freiza; 05-18-2012 at 01:26 PM.

  6. #6
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    1.The stack space is very much limited. So, for bigger objects, the heap is needed.
    2. You can dynamically allocated heap memory, unlike in stack.
    (well, you can using some os specific functions.. but it is not very useful, generally.)
    3. When you want to create something which will exist until you explicitly delete it.
    (you wouldn't want a window to disappear just because you returned from a function, would you ?)
    4. // I can't think of any more, atm. I'm sure someone will expand on this .
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    2,361
    in GUI systems like Qt, you don't want your window to disappear as soon as the function that created it returns... so you allocate it on the heap and hang on to a pointer to it.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,300
    Quote Originally Posted by freiza View Post
    Hence using stack increases exe size.
    Completely wrong. The stack size has nothing to do with the size of the compiled executable.

    There are plenty of resources online about what heap allocation is for.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,259
    Quote Originally Posted by freiza View Post
    Code:
    QLabel  *label=  new  QLabel("HelloQt!"); //this works
    
    QLabel  label=  new  QLabel("HelloQt!"); //Error: Cannot convert QLabel* to QLabel
    When this is possible. I mean, what must be class declaration of QLabel so that this thing can happen.
    To answer the original question (which I'm not sure anyone has done so far) the second form would require QLabel to support a non-explicit constructor and (preferably, in interests of completeness) assignment operator that accepts a pointer to QLabel.

    To avoid memory leaks, the implementation of those functions (and of other member functions of the class) would need to ensure cleanup of the dynamically allocated objects.

    Getting those things correct can be non-trivial which is why, in practice, such tricks are often a bad idea.

    The effect is probably not what you intend either. It will certainly not give behaviour that is possible in Java or C# in similar circumstances (at least, not without a lot of effort).

    Quote Originally Posted by freiza View Post
    And how can I see class declaration of QLabel in Qt?
    Seriously? How would you go about finding the class definition for any class that you have created?

    Find the header file that declares it, open that header file in a text editor, and scroll down to find the class definition of QLabel.
    Last edited by grumpy; 05-18-2012 at 06:58 PM.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie class question
    By vastgoten in forum C++ Programming
    Replies: 4
    Last Post: 07-31-2008, 01:43 AM
  2. Newbie class question
    By Beowolf in forum C++ Programming
    Replies: 1
    Last Post: 09-22-2007, 09:03 PM
  3. Newbie Pointer question
    By kook44 in forum C++ Programming
    Replies: 5
    Last Post: 01-02-2006, 12:07 PM
  4. Newbie Question: My class
    By jimboob in forum C++ Programming
    Replies: 5
    Last Post: 06-08-2004, 09:58 AM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 09:50 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21