🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Init an array in a constructor???

Started by
2 comments, last by illuna 24 years, 8 months ago

You could try initializing your variables in the body of the ctor, instead of the initialization list.

e.g:

MyClass::MyClass() : MyParent ( this ) {

m_array[0] = g_button_8;
m_array[1] = g_button_9;
m_array[2] = g_button_10;

m_other = 150;
m_other.add_callback(on_scroll, this);

}


HTH,

-mordell

__________________________________________

Yeah, sure... we are laughing WITH you ...
Advertisement
Out of curiousity how are you calling add_callback on an integer variable?
Hi!

Does anyone know how I could initialize an array in a constructor in VisualC++? My code looks like this:

// MyHeader.h
class MyClass: public MyParent{
int m_array[3];
int m_other;
}

//MySource.cpp
MyClass::MyClass():
MyParent(this),
m_array[0](g_button_8),
m_array[1](g_button_9),
m_array[2](g_button_10),
m_other(150){

m_other.add_callback(on_scroll, this);
}

This code initialize m_other to 150, add a callback to m_other and SHOULD initialize m_array's three slots to with the globals variable g_button_X.

I really need to initialize my variables this way in the constructor.

This works perfectly for the simple variable (ex. integer) but it generate errors with arrays.

Anyone know how to initialize an array's "slots" one by one this way?

Thanks.
Illuna


To mordell, yeah I already tried this but you have to know that this "m_array[3](g_button_8)" calls the constructor of m_array (yeah I know my example is not clear but m_array's type is an other class)

To JDudgeon, there's no question to be ask here since the code I wrote is not the real code it's just an example and I don't add any callback on this integer.

Illuna

This topic is closed to new replies.

Advertisement