Click here to Skip to main content
15,880,503 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have initialized as following:

fvariable[100] = {0.0, 0.0} in constructor but I got syntax error.


Is it not the right way to initialize it?
Posted

 
Share this answer
 
Comments
Olivier Levrey 7-Mar-11 6:03am    
good link
bunathangaraj 7-Mar-11 6:12am    
Thanks!
You cannot do that in a object constructor (if I got you). You have to explicitely initialize it inside the constructor block, for instance:
C++
class MyClass
{
  float fvariable[100];

public:
  MyClass()
  {
    for (int n=0; n < sizeof(fvariable)/sizeof(fvariable[0]); n++)
    {
      fvariable[n] = 0.0;
    }
  }
};

:)
 
Share this answer
 
v3
Comments
Olivier Levrey 7-Mar-11 6:03am    
Your solution works although your comment about constructor initialization is not correct.
CPallini 7-Mar-11 6:06am    
Why? Do you know how to initialize an array in class constructor initializer list? Please show the code, then.
bunathangaraj 7-Mar-11 6:11am    
Thanks CPallini. It works.
CPallini 7-Mar-11 6:17am    
You are welcome.
Olivier Levrey 7-Mar-11 6:11am    
Sorry, I misunderstood you. I didn't understand you were talking about initializer list.
Forget what I said then.
Maybe you just forgot the ;? And don't forget the leading f to tell the compiler you want a float and not a double.
This should work:
//declaration time
float fvariable[100] = { 0.0f };

//you can't do that.
//fvariable[100] = { 0.0f };

//Or you can do as CPallini suggests:
for (int i = 0; i < 100; n++)
    fvariable[i] = 0.0f;


But they are other ways...
 
Share this answer
 
v2
Comments
Alain Rist 7-Mar-11 10:01am    
[only the 2 first elements will be initialized!] is not true. If there are fewer items in the initializer list than elements in the array, the default constructor is used for the remaining elements. So float fvariable[100] = {0}; initializes the 100 values to 0.0f.
Olivier Levrey 7-Mar-11 10:02am    
Op is using C++ not C#. What you say is Ok for C#, not C++.
I observed different behaviors depending on the compiler while using "= { 0 };" declaration. Try this:
"float values[100] = { 5.0f };" and display values[1] in a message box.
On my compiler (Visual Studio 2008), it won't display "5".
Alain Rist 7-Mar-11 10:11am    
See http://msdn.microsoft.com/en-us/library/508fz12s(v=vs.80).aspx, or http://www.daniweb.com/forums/thread71414.html or any C++ tutorial. BTW I never used C# :)
Alain Rist 7-Mar-11 10:15am    
float values[100] = { 5.0f };" initializes values[0] to 5.0, and all others with the float 'default initializer' ie 0.f, strange to teach that to you ;)
Olivier Levrey 7-Mar-11 10:27am    
I did a few tests. It seems you are right. A few years ago I had a problem with such initialization and I found out that it didn't initialize all the array as I expected. I don't remember the details but I probably mixed my mind since then...
I will update correct my answer. Thanks.
Hi,
You should get acquainted with initializer lists[^] as they are generalized in C++0x.

To initialize a C array member in a C++ class constructor, (which is poor design), swap it with a default initialized one, for instance:
C++
#include <algorithm>
class MyClass
{
  float fvariable[100];

public:
  MyClass()
  {
      float temp[100] = {0};
      std::swap(fvariable, temp);
  }
};


If you use a modern C++ compiler (which you should) as VC2010 or gcc 4.5 replace your C array by a C++ std::array, and it will be default initialized:
C++
#include <array>
class MyClass
{
  std::array<float, 100> fvariable; // default initialized to all zeroes

public:
  MyClass()
  {}
};

cheers,
AR
 
Share this answer
 
Comments
CPallini 7-Mar-11 11:42am    
What is the advantage of swapping with the temporary (just curiosity)?
Alain Rist 7-Mar-11 12:08pm    
With move semantics swap does no copy in this case.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900