Wednesday, May 16, 2012

Initializing a static pointer in C++

I have a class with a static member that's a pointer like so :



animation.h



class Animation
{
public:
Animation();
static QString *m;

};


animation.cpp



#include "animation.h"

QString* Animation::m = 0;

Animation::Animation()
{
}


When I try to initialize that 'm' pointer from another class like so :



Animation::m = new QString("testing");


It works.



But when I do it this way :



QString x("Testing");
Animation::m = &x;


The program crashes.



What is wrong with this second method ?



Also I would like to have that static pointer as private so I can make static getter and setter functions to it. The setter should use the second method as the 'x' will come in a parameter so I'm stuck.



Thanks for any help!





No comments:

Post a Comment