[?] C++ Declarations

Hi friends, what the diference of this:

void Class::enable():setted(true){
}

and

void Class::enable(){
setted = true;
}

Thanks advance... and sorry for my english..

no difference for that example

Here’s an example where it makes a lot of difference:

#include <stdio.h>
struct foo {
  foo(int i) { printf("foo %d\n", i); }
};

class bar {
  foo foo1;
public:
  bar() { }
};
int main() {}

does not compile. g++ gives:

% g++ foo.cpp
foo.cpp: In constructor ‘bar::bar()’:
foo.cpp:9:9: error: no matching function for call to ‘foo::foo()’
   bar() { }
         ^
foo.cpp:9:9: note: candidates are:
foo.cpp:3:3: note: foo::foo(int)
   foo(int i) { printf("foo %d\n", i); }
   ^
foo.cpp:3:3: note:   candidate expects 1 argument, 0 provided
foo.cpp:2:8: note: foo::foo(const foo&)
 struct foo {
        ^
foo.cpp:2:8: note:   candidate expects 1 argument, 0 provided

while clang++ gives the much more helpful:

% clang++ foo.cpp
foo.cpp:9:3: error: constructor for 'bar' must explicitly initialize the member 'foo1' which does not have a default constructor
  bar() { }
  ^
foo.cpp:7:7: note: member is declared here
  foo foo1;
      ^
foo.cpp:2:8: note: 'foo' declared here
struct foo {
       ^
1 error generated.

The only way to initialize foo1 in bar is to use the initializer list, which is what your first example shows:

class bar {
  foo foo1;
public:
  bar() : foo1(3) { }
};

My advice is to use the initializer list whenever you are initializing a value, and avoid doing things like in your second example. The reason for this is that in your second example, setted is initialized twice – once to its default value of 0 and then again to true. Now the compiler easily optimizes that out, but if instead it was a class with a large constructor, the class could spend a lot of time setting its values to the default values and then spend even more time setting it to the values you actually want it to. Here’s an example of that:

#include <stdio.h>
struct foo {
  foo() { printf("foo()\n"); }
  foo(int i) { printf("foo(%d)\n", i); }
  int operator=(int i) { printf("operator=(%i)\n", i); return i;}
};

class bar {
  foo foo1;
public:
  bar() { foo1 = 4; }
};

class foobar {
  foo foo1;
public:
  foobar() : foo1(4) { }
};

int main() {
    printf("Initializing instance of bar:\n");
    bar bar1;
    printf("Initializing instance of foobar:\n");
    foobar foobar1;
}

This produces:

% ./a.out
Initializing instance of bar:
foo()
operator=(4)
Initializing instance of foobar:
foo(4)

As you can see, when bar is initialized, 2 of foo’s methods are called, while when foobar is initialized, just one of foo’s methods are called. This clearly shows how initializer lists are better to use when dealing with classes with constructors, and it’s better to be consistent and always use them.

what the diference of ...

Effectively, none. Now, if the variable being initialized was a complex type, like a class, and the class was the base class, then there are subtle differences that may not allow for the second form. But, for simple types, there is no difference.

Unless I misread it, the OP was not asking about constructors.

This code:

struct myClass 
  {
  int setted;
  myClass () {  }
  void enable ();
  };

void myClass::enable () : setted (true) {}

int main() {}

Fails to compile with:

sketch_jul30b.ino: In member function 'void myClass::enable()':
sketch_jul30b:8: error: only constructors take base initializers

However this compiles:

struct myClass 
  {
  int setted;
  myClass () {  }
  void enable ();
  };

void myClass::enable () 
  {
  setted = true;
  }

int main() {}

So my answer is “one compiles, one doesn’t”.

Thanks friends... I'm using multiples derived classes, so the issue was that.

WizenedE

Thank you so much. ;D