Gluster Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
1
of 1 vote

Apart from initialsing constant variables, is there any scenario where we should be using class initialsiation list instead of constructors?
Is it a proper method to use class initialsiation list instead of constructors for normal variables

- xing2785 February 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, you can use the initialization list to initialize the normal (non-const, non-static) members. Infact, this is a more efficient way of initializing class members.

Other situations where you need to use the initialization list are:
1. Initializing references.

Class A;
Class B
{
  private:
     A& ref;
  public 
     B(const A& ref): ref(ref)
     {

     }
}

2. Need to initialize some members of the parent class (if the parent class has a parameterized constructor).
3. Const members.

- Anonymous May 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The const variable specifies whether a variable is modifiable or not. The constant value assigned will be used each time the variable is referenced. The value assigned cannot be modified during program execution. class is typically declared in a header file and a header file is typically included into many translation units. However, to avoid complicated linker rules, C++ requires that every object has a unique definition. That rule would be broken if C++ allowed in-class definition of entities that needed to be stored in memory as objects. A const variable has to be declared within the class, but it cannot be defined in it. We need to define the const variable outside the class.
-HTH

- hk18 February 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

myclass() : i(10)
Here the i = 10 assignment in initializer list happens much before the class initilizaiton occurs.
 
myclass(
{
     i = 10
}
Here the assignment happens after initializing the class, once this is initialized you cant modify the const variable.

- Anonymous February 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A (non-static) const variable in a class is required to be initialized before initialization of the object of that class. The class initialization list is the only place where such a variable can be initialized cause it occurs prior to execution of the constructor.

- Anonymous February 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This thing has been explained very nicely in Effective c++ by Scott Mayer. In the first case say
myclass(int a=10)
{
i = a; // Its assignment not initialization
}
Here First the default constructor is called and then the value is again assigned to i by calling the copy constructor, thus wasting the work done by default constructor. Initialization mean the value with which the variable is born. To ensure that we make use of member initialization list.
myclass(int a=10)::i(a)
{
// This is called initialization
}
This ensures that the variable is born with the given value. in previous case the value of constant was getting modified on being the copy constructor called. But the member initialization list solves that problem. This is very effective when we require to initialize the user defined data members.

- Anurag Jain June 30, 2011 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More