|
-
March 19th, 2011, 07:06 AM
#1
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
-
March 19th, 2011, 07:37 AM
#2
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.
-
March 19th, 2011, 07:37 AM
#3
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.
-
March 19th, 2011, 12:57 PM
#4
Re: creating variables on the stack
 Originally Posted by aamir121a
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:
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.
-
March 19th, 2011, 01:04 PM
#5
Re: creating variables on the stack
 Originally Posted by ninja9578
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!
-
March 19th, 2011, 10:31 PM
#6
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
-
March 20th, 2011, 06:11 AM
#7
Re: creating variables on the stack
-
March 20th, 2011, 06:17 AM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|