CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    creating variables on the stack

    Can some please show me , how to create class instances on the stack , the ones I am interested in are the ones which need to have arguments passed on the constructor ,
    I use QT as C++ dev environment ,

    Code:
    QString mystring;  // that is good  , as you can see the class instance do not require a argument to be // passed on to the constructor 
    // how to do this in the header file
    
    QPainter mypainter(this);
    when ever their is a argument to be passed passed on the constructor I end up doing this;

    QPainter *mypainter; // in the header

    and then in CPP file

    mypianter = new QPainter(this);

    I was wondering if there was a way to this on the stack as oppose to the heap.
    saves me the trouble of deteting the class instance later

    as in
    Code:
    detete mypainter;

  2. #2
    Join Date
    Jan 2009
    Posts
    1,689

    Re: creating variables on the stack

    I'm not 100% sure, but I think this is acceptable:

    Code:
    QPainter painter;
    
    ...
    
    new(&painter) QPainter(this);
    But if QPainter is inheritable, I would create a sub-class where you can just call a seperate method to reinitialize.

  3. #3
    Join Date
    Aug 2009
    Location
    Romania->Felnac
    Posts
    48

    Re: creating variables on the stack

    You can create object on the stack and pass parameters to them, there is no problem there:
    Code:
    QString stringName("Whatever string you like");
    The only "problem" is when you want the default constructor (without arguments) then you can't have () for default constructor:
    Code:
    QString str0; //ok create an empty QString
    QString str1(); //compiler thinks that you want a function: 
    //named str1 that doesn't take parameters and returns a QString
    LE: With Qt you need to be careful about the objects that you don't need to delete (the parent-child relationship)
    Last edited by Zlatomir; March 19th, 2011 at 07:39 AM.

  4. #4
    Join Date
    Apr 2008
    Posts
    725

    Re: creating variables on the stack

    Quote Originally Posted by aamir121a View Post
    Can some please show me , how to create class instances on the stack , the ones I am interested in are the ones which need to have arguments passed on the constructor ,
    I use QT as C++ dev environment ,

    Code:
    QString mystring;  // that is good  , as you can see the class instance do not require a argument to be // passed on to the constructor 
    // how to do this in the header file
    
    QPainter mypainter(this);
    when ever their is a argument to be passed passed on the constructor I end up doing this;

    QPainter *mypainter; // in the header

    and then in CPP file

    mypianter = new QPainter(this);

    I was wondering if there was a way to this on the stack as oppose to the heap.
    saves me the trouble of deteting the class instance later

    as in
    Code:
    detete mypainter;
    Code:
    QPainter painter(this);
    or

    Code:
    QPainter painter;
    painter.setParent(this);

    Code:
    class someclass
    {
    public:
      someclass(QWidget* parent = 0);
      QPainter m_painter;
    };
    
    someclass::someclass(QWidget* parent )
    : m_painter(parent)
    {
    }
    Last edited by Amleto; March 19th, 2011 at 01:00 PM.

  5. #5
    Join Date
    Apr 2008
    Posts
    725

    Re: creating variables on the stack

    Quote Originally Posted by ninja9578 View Post
    I'm not 100% sure, but I think this is acceptable:

    Code:
    QPainter painter;
    
    ...
    
    new(&painter) QPainter(this);
    But if QPainter is inheritable, I would create a sub-class where you can just call a seperate method to reinitialize.
    That doesnt look good - it overwrites an object that already exists!

  6. #6
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    Re: creating variables on the stack

    QPainter was just an example how about objects which cannot be instanced without passing some arguments to the constractor

  7. #7
    Join Date
    Apr 2008
    Posts
    725

    Re: creating variables on the stack

    I already showed you

  8. #8
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    Re: creating variables on the stack

    thank you all, your post have been most helpful
    Last edited by aamir121a; March 20th, 2011 at 06:21 AM. Reason: typo

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured