Global variable initialization in a class

Need help with C, C++, perl, python, etc?

Global variable initialization in a class

Postby germain » 2007-05-03 17:02

If I have a code like this:
Code: Select all
class MiClase
{
      int a = 1;

      public:
           int getA(void);
};

int MiClase::getA()
{
     return a;
}


The compiler throws this error:
"ISO C++ forbids initialization of member a"

How can I initialize a global variable in a class? that was just an example, but what if I need to define an object wich needs to be visible from all the class members? instead of
Code: Select all
int a = 1;

it could change to
Code: Select all
object MyObject(param1, param2);


Do I need to use ponters?
User avatar
germain
 
Posts: 90
Joined: 2006-09-25 23:10
Location: Mexico. Ajuuuua!!!

Postby matino » 2007-05-03 17:55

Initialization is possible only through the constructors:
http://www.fredosaurus.com/notes-cpp/oo ... ctors.html

If you want to use a global variable that is visible from all objects just put it outside the class:
Code: Select all
int a = 1;
class MyClass {
....
}
User avatar
matino
 
Posts: 79
Joined: 2007-02-24 17:03

Postby germain » 2007-05-03 18:13

But I want to use a distinct "a" variable for every object, if I define it outside the class I only have one "a" variable that is common for all the objects, in that case maybe I could use static members of the class, wich I don't need
User avatar
germain
 
Posts: 90
Joined: 2006-09-25 23:10
Location: Mexico. Ajuuuua!!!

Postby matino » 2007-05-04 10:47

Then just use variable inside the class and initialize it every time you create an object. I don't really get what you are trying to do, maybe you could explain a lil more ?
User avatar
matino
 
Posts: 79
Joined: 2007-02-24 17:03

Postby germain » 2007-05-04 14:32

That is ok in the case of "a", but I want to use some objects whose constructor takes some arguments, so I can't initialize them as members of the class.

But I almost find the way: Initializers lists:

Code: Select all
class MyClass
{
       object MyObject;  //Here "object" takes exactly two arguments but I don't use the constructor here

       public:
            MyClass();
};

MyClass::MyClass(): MyObject(0, 1)
{

}


Now my problem is: How can I initialize arrays using initializer lists? (Because MyObject needs an array to use in its constructor)

Doesn't work "array[0] = 0, array[1] = 1" and "array(0, 1)" either
User avatar
germain
 
Posts: 90
Joined: 2006-09-25 23:10
Location: Mexico. Ajuuuua!!!

Postby matino » 2007-05-04 17:38

The same way you pass it to other functions - use pointer (in this case the name of array) :)
User avatar
matino
 
Posts: 79
Joined: 2007-02-24 17:03

Postby germain » 2007-05-04 20:50

After some reading I found that I cannot initialize arrays in the initialization list directly:

http://www.thescripts.com/forum/thread607633.html

So I use the trick of initialize it with "zeros" and after that modify it inside the class constructor:

Code: Select all
class MyClass
{
       int array[2];
       object MyObject;  //Here "object" takes exactly two arguments but I don't use the constructor here

       public:
            MyClass();
};

MyClass::MyClass(): array(),  MyObject(0, array)
{
     array[0] = 10;
     array[2] = 20;
    MyObject.changeInitData(0, array);
}
User avatar
germain
 
Posts: 90
Joined: 2006-09-25 23:10
Location: Mexico. Ajuuuua!!!


Return to Programming

Who is online

Users browsing this forum: No registered users and 5 guests

fashionable