Do you use C++11

Pages: 123... 8
I use stuff like iterator_lists/range based for loops/ small stuff like that.

Code::Blocks with mingw doesn't support initializing with a value in the header of a class :(

1
2
3
4
5
class why
{
public:
    int itnowork=5;
};


doesn't work
Use constructors/destructors.
Thanks Sherlock.

That is like saying instead of doing this:
int i = 55;

you should do this:

1
2
int i;
i=55;
Last edited on
Yes I do. My current projects are all C++11
No because VC++ 2010 is being a "D" and whenever I attempt to do what you're trying to accomplish it doesn't let me.

Sometimes when I'm working with SDL_Color's it wont let me do this SDL_Color black = {0, 0, 0, 0};
Yeah if I'm working with C++ it's C++11.
@Fred: Visual Studio is falling behind on its C++11 support, which sucks since for a Windows IDE I think it's unparalleled.
VS2010 supports very little c++11, VS2012 supports much more although it is not complete yet. People should remember that unlike clang and gcc the VS team focus on stability rather then rushing to include new features. I would love if VS was fully c++11 compliant, but I'm willing to wait.
@alitcandle
you can just do

myclass myinstance = 5

With constructors, you don't need to do that.

example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class myclass{
public:

    //public constructors
    myclass(); //no args
    myclass(const int&); //set it equal to an int...

    ~myclass();

    //rest here

private:

    int mystoredint;  //constructors will assign a value to this accordingly
};


//in the cpp file:

myclass::myclass(const int& i)
{
    myclass::mystoredint = i;
}

myclass::myclass()
{
    myclass::mystoredint = 0;
}

myclass::~myclass()
{
}

//in a function:

myclass duh = 5;
//now the class = 5.

myclass duh; //the no-arg constructor is called, setting the class to 0, it's default. 


You can't do that?
Last edited on
@IWishIKnew
C++11 adds in class member initialization so this is valid.
1
2
3
4
5
6
class A
{
    int a = 10;
    public:
    A(){}
};
@naraku

I don't usually mess with setting defaults. I just pass by address with constants, and overload if I really need a place where I don't want to pass a variable. This way, I know that the compiler will catch my errors in the passing or arguments. I had a LOT less syntactical issues since I started doing that.

In short, I am not familiar with in-class initialization of defaults.

I suppose whatever floats you boat though.
Last edited on
closed account (zb0S216C)
AlitCandle wrote:
"Do you use C++11"

No. There's no need.

Wazzak
@AlitCandle upgrade MinGW. Latest version support class member initialization.
closed account (1yR4jE8b)
There's no need.


Maybe so, but it sure made my life a hell of a lot easier when I started using it.
@AlitCandle

Code::Blocks with mingw doesn't support initializing with a value in the header of a class :(

When it comes to C++03, if "itnowork" is a constant value, then you can initialize in within the body of the class like this:

1
2
3
4
5
class why
{
public:
    static const int itwork=5;
};


if non-const, you should use the constructor initializer list

1
2
3
4
5
6
7
class why
{
public:
    int italsowork;

    why() : italsowork(5) { }
};


Andy
Last edited on
What I'm trying to figure out is why people want to define constant values in-class when they can just use #define s
closed account (3qX21hU5)
http://stackoverflow.com/questions/1637332/static-const-vs-define

Last edited on
You don't need to tell me that Andy. I know c++ syntax(with the exception of some c++11 and standard library functions) like the back of my hand because I just read a book on it.
Last edited on
I also don't see why you should use constructor initializer lists. IIRC all they do is make sure those objects are constructed before the object that is being constructed. I'm pretty sure this doesn't make a difference when using primitive types like integers.
closed account (3qX21hU5)
AlitCandle wrote:
Thanks Sherlock.

That is like saying instead of doing this:
int i = 55;

you should do this:


1
2
int i;
i=55;


Actually that is nothing like doing that. When you use a constructor initializer list you are directly initializing a value to the member not assigning a value.

That is why you can assign a value to a const member when you use constructor initializer list.

I still don't understand why anyone would want to do that, even more why anyone would want to use C++ 11
Pages: 123... 8