Const Pointer and Const Value in Class

My Class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//H-File

class My_Class
{
     const QStringList const* returnReference();
};

//Cpp-File
const QStringList const* My_Class::returnReference()
{
     QDir *dir = new QDir("/");
     const QStringList const* strList = new QStringList(dir->entryList(QDir::NoDotAndDotDot));

     return strList;
}


So this Code doesn't go.
error: duplicate ‘const’


But a Reference would also not be possible, because then I would return a empty reference. (The QStringList would be deleted afer returning from returnReference())

My Problem is that QDir has no other function. (to gain a Pointer or something on QStringList) So have I overseen something or am I not able to make it const?(const pointer and const state)
'const T *' and 'T const *' mean the same. Perhaps you meant 'const T * const', which is a constant pointer to a constant object. Although I don't see any reason for the pointer itself to be constant.

The QStringList would be deleted afer returning from returnReference()
Not if the reference points to a dynamic object.
move the second const after the star: const QStringList *const

(Edit: Too Slow)
Last edited on
Ohhh, my mistake. I never used this before. But now it works perfectly. Although the syntax of these two const is a little weird. The first const is before the Type, but the second is after the Type ("Pointer").
The pointer should be const that I can't change it. Const is therefore I think, or not? ;)

Thank you.
Last edited on
Are you sure you want to do this? That is, declare such a pointer as the return type of a public interface of a class? Have you thought about how someone might use such a thing.
Although the syntax of these two const is a little weird.

It makes more sense if you read it backwards:

1
2
const int* pX;    // changeable pointer to constant int
int* const pY;    // constant pointer to changeable int 
Topic archived. No new replies allowed.