Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi all,

I read in some articles that there is something called Initializing blocks in java ; where we can perform some initializing assignements When the Class is loaded or an instance is created.

C#
class InitDemo
{
    static int y;
    int x;
     
     {
       y=10;
       x = 0;
     }
}


I am asking is there is such paradigme in C++ ?

Thank you.
Posted

I would no call it a "paradigm"; this is just a tiny syntactic feature. It looks like StackOverflow already gave you an answer; please check it.

I need to add something to this answer. First, instance (non-static) fields are recommended to be initialized in C++ initialization statement, which applies only to constructors and is important fool-proof syntactic feature. See how x(0) initializes x:

C++
class InitDemo {
    static int y;
    int x;
public:
    InitDemo() : x(0) { }
};

// this line cannot be in a header file:
int InitDemo::y = 10;


The problem is initialization of a static field. Of course, you could initialize it in the constructor, but the result of it would not be what you want. Consider:
C++
class InitDemo {
    static int y;
    int x;
public:
    InitDemo() : x(0) {
        y = 10; // WARNING! Don't repeat at home (unless you know exactly what are you doing :-)
    }
};


It works at least once, but imagine what happens if you want to accumulate some value across all the instances of this class. In this case, each constructor would reset the modified value of y to 10, effectively cancelling the result of previous calculations.

So, what do you think? Looks ugly compared to Java? And in .NET, there are very handy static constructors perfect for the purpose. I personally consider C++ as a ridiculously archaic language (despite its apparent success), but there are too many C++ fans ready to scratch out my eyes for such words… I'm prepared for down-votes… :-)

—SA
 
Share this answer
 
v3
Comments
Code-o-mat 9-May-12 14:14pm    
Even though i am a C++ "fan", here's a 5 for your answer. :) (Btw I agree somewhat, in some things C++ is indeed a bit...old style)
Sergey Alexandrovich Kryukov 9-May-12 14:21pm    
Great to hear from a reasonable software developer who certainly needs to have wider views. :-)
Thank you,
--SA
In the/a header file:
class InitDemo
{
  static int y; //Declaration of static member variable 'y'
  int x; //Declaration of member variable 'x'
  
  InitDemo(); //Declaration of constructor
};


in the/a cpp file:
int InitDemo::y = 1; //Initialization of static member variable 'y'

InitDemo::InitDemo() //Implementation of constructor
{
  x = 2; //initialization of member variable 'x'
}


Does this answer your question?
 
Share this answer
 
Comments
Schehaider_Aymen 9-May-12 12:26pm    
I afraid that it is not what i am looking for, i am aware about the constructors.

below what is written in the article that i read:

Apart from methods and constructors, Initialization Blocks are the third place in a Java Program where operations can be performed.


in the Initializing block code is written between braces with no suffix


{
y =10;
x=0;
}
Code-o-mat 9-May-12 13:10pm    
Then i guess, as Schehaider_Aymen also said, there's no such thing in C++ that i know of.
Sergey Alexandrovich Kryukov 9-May-12 13:49pm    
Please also see my answer with some extra explanations.
--SA
Sergey Alexandrovich Kryukov 9-May-12 13:48pm    
Correct, but not the best way to initialize x; I voted 4 -- please see my answer.
--SA
Code-o-mat 9-May-12 14:10pm    
Thanks for your vote. What the "best" way is is really a question of personal taste. I avoided that form of initialization for the sake of clarity, since "x = 10" is pretty obvious, but "x(10)" might look confusing at first. I heard that at some places they even discourage you from using this form for initialization with simple types because at the first look it suggests that you are constructing some more complex object and it makes your code harder to decyphwer when others look at it. Nevertheless, i should have mentioned it at least.
Ok, based on another forum, i guess that such concept does'not exist in C++.

It's nice to learn new things :)
 
Share this answer
 

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