Asked  7 Months ago    Answers:  5   Viewed   39 times

Internally and about the generated code, is there a really difference between :

MyClass::MyClass(): _capacity(15), _data(NULL), _len(0)
{
}

and

MyClass::MyClass()
{
  _capacity=15;
  _data=NULL;
  _len=0
}

thanks...

 Answers

95

Assuming that those values are primitive types, then no, there's no difference. Initialization lists only make a difference when you have objects as members, since instead of using default initialization followed by assignment, the initialization list lets you initialize the object to its final value. This can actually be noticeably faster.

Tuesday, June 1, 2021
 
IcedAnt
answered 7 Months ago
47

Reread the paragraphs near there describing what each of the parameters are. Specifically, it should mention that i and j are not values, but iterators. This constructor is very commonly used to make copies of other types of containers. If you want to get a sequence of values, the Boost library provides a counting iterator, that does exactly what you want.

std::vector<unsigned int> numbers(
     boost::counting_iterator<unsigned int>(0U),
     boost::counting_iterator<unsigned int>(10U));
Tuesday, August 3, 2021
 
Michael Emerson
answered 5 Months ago
49

Neither one is better from a language point of view since they are the same, array indexing should just be syntactic sugar for pointer arithmetic. We can see this from the draft C99 standard section 6.5.2.1 Array subscripting which says (emphasis mine):

[...]The definition of the subscript operator [] is that E1[E2] is identical to (*((E1)+(E2))).[..]

Although for others reading your code a[i] is probably more readable.

Thursday, August 5, 2021
 
Axalix
answered 5 Months ago
97

The logical operator works on booleans, and the bitwise operator works on bits. In this case, the effect is going to be the same, but there are two differences:

  1. The bitwise operator is not meant for that, which makes it harder to read but most importantly
  2. The logical OR operator will evaluate the first condition. If it's true, it does not matter what the next condition results in, the result will be true, so the second clause is not executed

Here's some handy code to prove this:

public class OperatorTest {

    public static void main(String[] args){
        System.out.println("Logical Operator:");
        if(sayAndReturn(true, "first") || sayAndReturn(true, "second")){
            //doNothing
        }

        System.out.println("Bitwise Operator:");
        if(sayAndReturn(true, "first") | sayAndReturn(true, "second")){
            //doNothing
        }
    }

    public static boolean sayAndReturn(boolean ret, String msg){
        System.out.println(msg);
        return ret;
    }
}
Sunday, August 8, 2021
 
Gabriele
answered 4 Months ago
44

Member variables are initialised in the order that they are declared in the class declaration (even if you have them in a different order in the initialisation list of the constructor), so yes, by the time c is being initialised, b will be initialised, and you can use b to initialise c.

As Ricardo Cardenes notes, it will still work even if you declare c before b in the class definition (which means you will pass C::C a reference to an uninitialised B) however you cause undefined behaviour if you use the object inside C::C. It's safer to declare b first, because although you may not use b inside C::C now, you might in the future and forget that the reference refers to an uninitialised B, and cause UB.

And no, you do not have to explicitly initialise b (unless it is POD) unless you don't want it to be default-constructed. So this code would be what you want (again, if B isn't POD):

A::A() : c(b) { }
Wednesday, November 17, 2021
 
Samir Sabri
answered 3 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share