Static Arrays
Jun 11, 2013 at 3:38pm UTC
Hello.
I want a class to store some arrays, I don't want to (or need to) instantiate this class, since its arrays will be unique. So I guess I can create them as static, and use them all over my program without instantiate the class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
// a.h
class A {
public :
explicit A();
int * getB();
protected :
void updateB();
private :
static int B[100];
signals:
public slots:
void MySLot();
};
// a.cpp
#include "a.h"
int B[100] = {0};
A::A(){
}
int * A::getB(){
int * pointer = &B[0];
return pointer;
}
void A::MySLot(){
void updateB();
}
Basically whenever I want to access B array, I just call A::getB() and get a pointer as return.
Is this correct?
Thanks in advance.
Last edited on Jun 11, 2013 at 3:39pm UTC
Jun 11, 2013 at 3:52pm UTC
A::MySlot ,
A::getB and
A::updateB require an instance to be instantiated.
Why not:
1 2 3 4 5
//a.h
namespace A {
int * getB() ;
void updateB() ;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//a.cpp
#include "a.h"
namespace {
int B[100] = {0} ;
}
int * A::getB()
{
return B ;
}
void A::updateB()
{
// ...
}
Jun 13, 2013 at 11:23pm UTC
I didnt use namespace, but i solved my problem, thanks anyways!
Topic archived. No new replies allowed.