PDA

View Full Version : Initialize a non-static array of pointers in constructor



Wer_Bn
17th August 2015, 12:27
Hello guys

.h


#ifndef A_H
#define A_H

#include <QObject>
#include <QList>

class A : public QObject
{
Q_OBJECT
private:

private:

void first();
void second();
void third ();
// and so on

void(A::*handlers[4])(void);

public:

A();
};
#endif // A_H


.cpp


#include "a.h"

void A::first()
{

}

void A::second()
{

}

void A::third()
{

}

A::A() : handlers {&A::first, &A::second, &A::third, 0}
{
//this is ugly
handlers[0] = &A::first;
handlers[1] = &A::second;
handlers[2] = &A::third;

//this would be nice
//handlers[4] = {&A::first,&A::second,&A::third,0};//in static this would work, because it would be like redeclaration, with the type speficier behind
}


You see, this code gives me an error.
I found out that this error does not happen when compiling the code using g++ with -std=c++11 in Cygwin.
Also, by searching on internet, I found out that this should work.

I want to initialize that array of pointers in the constructor, but it gives me this error:
Syntax error - Missing ; before }

Already placed parentheses, to look like this:

A::A() : handlers ({&A::first, &A::second, &A::third, 0})

But it gives me a warning and an error:
Syntax error - missing ) before {
Warning - The elements of the array "A :: Handlers" are by default "initialized.

Is this a Qt problem?
Thanks

ChrisW67
17th August 2015, 13:26
Is this a Qt problem?
No, probably not.

Your code, as posted without the parentheses, compiles fine here (Linux, gcc 4.8.4) if:


CONFIG += c++11

is in your Qt5 pro file.



I want to initialize that array of pointers in the constructor, but it gives me this error:
Syntax error - Missing ; before }

What is "it"? I assume Windows, but a Microsoft compiler, MingW, ...?

Wer_Bn
17th August 2015, 13:44
Sorry, it's MSVC2010.
And, it doesn't work. Even with that flag.

ChrisW67
17th August 2015, 21:35
Support For C++11/14/17 Features (Modern C++)
https://msdn.microsoft.com/en-us/library/hh567368.aspx#corelanguagetable
Seems your compiler lacks support.

Wer_Bn
18th August 2015, 07:12
Ok
That list states that 2013 already has initializer lists.
Can I use Qt with MSVC2013?

Wer_Bn
18th August 2015, 11:02
Hey!
Finally was able to set Qt with MSVC2013.
But now the error is:
cannot specify explicit initializer for arrays

Even with parantheses, this happens
:(
I guess it's not possible

ChrisW67
18th August 2015, 21:36
Can you:


handlers = {&A::first, &A::second, &A::third, 0};

In the body of the constructor and leave it out of the initializer list.

Wer_Bn
19th August 2015, 12:54
"an initializer-list cannot be used as the right operand of this assignment operator"

:(

ChrisW67
19th August 2015, 20:31
Last thought,


void(A::*handlers[4])(void) {&A::first, &A::second, &A::third, 0};

This is "Non-static data member initializers" but I have not tested the syntax.

Wer_Bn
20th August 2015, 07:56
Ok
I guess you forgot about the '='

It's a really complicated syntax, it looks like you are delaring it again, just like static...
Anyway, I placed that in the constructor. But now there's a new warning:
'handlers' : local variable is initialized but not referenced

I'm actually using the function pointer in another method of the class!

anda_skoa
20th August 2015, 09:32
Anyway, I placed that in the constructor.

No, in the header.



But now there's a new warning:
'handlers' : local variable is initialized but not referenced

I'm actually using the function pointer in another method of the class!
You cannot use a variable that is local to the constructor's scope in another method. It doesn't exist in that other scope.

Cheers,
_

Wer_Bn
24th August 2015, 09:21
Ok
I know that. The thing is that "handlers" is a private variable of the class.
I know what's happening in this particular case: I'm declaring a local variable name "handlers", not initializing the private variable "handlers".

I cannot use that syntax.

anda_skoa
25th August 2015, 07:51
I cannot use that syntax.
So it doesn't work when you apply it in the header?

Cheers,
_

Wer_Bn
25th August 2015, 09:16
I have the header file just like posted (1st post).
The variable "handlers" is indeed in the header, as a private member of class "A".

But when I use that syntax in the constructor, it declared a local function pointer array named "handlers". Thus, the warning.

EDIT: I missed your warning: "No, in the header"

So, I placed the assignment outside of the class scope, in the header, and I get:
'A::first' : cannot access private member declared in class 'A'
'A::second' : cannot access private member declared in class 'A'
'A::third' : cannot access private member declared in class 'A'

If I do that in the class scope:
error: C2276: '&' : illegal operation on bound member function expression