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
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.
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)
Re: creating variables on the stack
Quote:
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)
{
}
Re: creating variables on the stack
Quote:
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!
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
Re: creating variables on the stack
Re: creating variables on the stack
thank you all, your post have been most helpful