Initialise const struct in a class constructor? [solved]

Mar 29, 2010 at 1:54pm
Estimated C++ programmers,
if I wish to have a struct object icons like this:
1
2
3
4
5
6
const struct
{
   QIcon −
   QIcon +
   QIcon &exit;
} icons;

into my class Class, then how should I initialise that struct upon the construction of Class? A single temporary object is created like this:
QIcon(":/images/exit.png")?
Last edited on Mar 29, 2010 at 2:55pm
Mar 29, 2010 at 1:58pm
uh.. I don't know whether you can have an unnamed struct with a constructor at all. Just make a named struct out of it and assign them in the initializer list of your constructor:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct IconsType // IconsType is the name of the struct. You used no name here.
{
   QIcon −
   QIcon +
   QIcon &exit;

   IconsType() // constructor.
      : minus("/images/minus.png")
      , plus("/images/plus.png")
      , exit("/images/exit.png")
   {}
};

// now you can allocate the global variable "icons" as const, if you want to.
const IconsType icons;


Of course, you should probably move the constructor to a cpp file.

Ciao, Imi.
Mar 29, 2010 at 2:01pm
Wait a second.... You are using references in the struct. Change them to normal icons.

1
2
   // This is a reference to a QIcon. But where is the real QIcon this is referring to?
   QIcon −


You may probably want this instead:

1
2
   // Aha. The icon is just part of this struct.
   QIcon minus;


Ciao, Imi.
Mar 29, 2010 at 2:02pm
So I can't initialise elements of struct from Class, just from within what you call IconsType?
Mar 29, 2010 at 2:21pm
So I can't initialise elements of struct from Class, just from within what you call IconsType?


I don't quite get what you are trying to achieve. If you are sure, that "QIcon&" is correct and you want to have a reference to another QIcon, then this has to be stored somewhere.

If your variable "icons" is a global variable, then this is not really possible, since you can not really guarantee in which order global variables are initialized.


Hm.. QIcon belongs to QT, right? IIRC, QT has some kind of memory management build-in, and you have to allocate the objects using "new QIcon(...)" anyway.

If you really speak about QT, I think I better step back and let someone else explain. I haven't worked with QT for a very long time and when I did once, the memory stuff there always confused me.

Ciao, Imi.
Mar 29, 2010 at 2:46pm
Solved now.

In class decl:
1
2
3
4
5
6
7
8
const struct Icons
{
   QIcon minus;
   QIcon plus;
   QIcon exit;

   Icons();
} icons;


In cpp:
1
2
3
4
5
6
Class::Icons::Icons():
minus(":/images/minus.png"),
plus(":/images/plus.png"),
exit(":/images/exit.png")
{
}


It worked also with QIcon − and *(new QIcon(...)).

What is still not clear: is that completely impossible to make a C like struct and initialise its members in the very same line they get declared?
Mar 29, 2010 at 3:07pm
What is still not clear: is that completely impossible to make a C like struct and initialise its members in the very same line they get declared?


Yep - that won't work. Only exception are "const static int".

But the C++ guys are working on that. C++0x - the new standard will allow for assigning values directly in the line where you declare things.

Ciao, Imi.
Mar 29, 2010 at 3:15pm
Thanks imi...
Mar 31, 2010 at 3:32pm
I think you need to separate out the declaration of the struct from the storage itself, comme ca.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class QIcon {} ;

struct Icons
{
	QIcon minus;
	QIcon plus;
	QIcon exit;
};

const struct Icons icons = { QIcon(), QIcon(), QIcon() };

int main()
{
}
Mar 31, 2010 at 3:40pm
Or since a struct is really a class with public members by default, you could add a constructor, et voila.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class QIcon {} ;
struct Icons
{
	Icons(const QIcon& minus_, const QIcon& plus_, const QIcon& exit_) {}

	QIcon minus;
	QIcon plus;
	QIcon exit;
};

class someClass
{
	someClass()
		: icons(QIcon(), QIcon(), QIcon()) {}

	const Icons icons;
};


int main()
{
}
Topic archived. No new replies allowed.