Initialize a non-static array of pointers in constructor
Hello guys
.h
Quote:
#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
Quote:
#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:
Quote:
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
Re: Initialize a non-static array of pointers in constructor
Quote:
Is this a Qt problem?
No, probably not.
Your code, as posted without the parentheses, compiles fine here (Linux, gcc 4.8.4) if:
is in your Qt5 pro file.
Quote:
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, ...?
Re: Initialize a non-static array of pointers in constructor
Sorry, it's MSVC2010.
And, it doesn't work. Even with that flag.
Re: Initialize a non-static array of pointers in constructor
Support For C++11/14/17 Features (Modern C++)
https://msdn.microsoft.com/en-us/lib...elanguagetable
Seems your compiler lacks support.
Re: Initialize a non-static array of pointers in constructor
Ok
That list states that 2013 already has initializer lists.
Can I use Qt with MSVC2013?
Re: Initialize a non-static array of pointers in constructor
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
Re: Initialize a non-static array of pointers in constructor
Can you:
Code:
handlers = {&A::first, &A::second, &A::third, 0};
In the body of the constructor and leave it out of the initializer list.
Re: Initialize a non-static array of pointers in constructor
"an initializer-list cannot be used as the right operand of this assignment operator"
:(
Re: Initialize a non-static array of pointers in constructor
Last thought,
Code:
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.
Re: Initialize a non-static array of pointers in constructor
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!
Re: Initialize a non-static array of pointers in constructor
Quote:
Originally Posted by
Wer_Bn
Anyway, I placed that in the constructor.
No, in the header.
Quote:
Originally Posted by
Wer_Bn
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,
_
Re: Initialize a non-static array of pointers in constructor
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.
Re: Initialize a non-static array of pointers in constructor
Quote:
Originally Posted by
Wer_Bn
I cannot use that syntax.
So it doesn't work when you apply it in the header?
Cheers,
_
Re: Initialize a non-static array of pointers in constructor
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