|
10-22-2016, 06:01 AM
|
#1
|
Human being with feelings
Join Date: Oct 2007
Location: home is where the heart is
Posts: 12,110
|
initializing members during construction
Does it make a difference if I init member variables via initialization list or in the body of the constructor ?
|
|
|
10-22-2016, 10:07 AM
|
#2
|
Human being with feelings
Join Date: Feb 2007
Location: Oulu, Finland
Posts: 8,062
|
With trivial things like ints or floating point numbers there won't be really be a difference. It is however good style to use the constructor init list.
Additionally, since C++11 you can also init member variables directly in the declaration of the member.
Code:
class MyClass
{
public:
private:
int m_foo{42};
double m_bar{-1.0};
std::string m_name{"MyClass"};
};
__________________
I am no longer part of the REAPER community. Please don't contact me with any REAPER-related issues.
Last edited by Xenakios; 10-22-2016 at 10:17 AM.
|
|
|
10-22-2016, 12:46 PM
|
#3
|
Human being with feelings
Join Date: Oct 2007
Location: home is where the heart is
Posts: 12,110
|
Thanks Xenakios.
The C++11 method is new to me.
|
|
|
10-23-2016, 02:50 AM
|
#4
|
Human being with feelings
Join Date: Jul 2008
Location: The Netherlands
Posts: 3,653
|
Quote:
Originally Posted by Xenakios
Additionally, since C++11 you can also init member variables directly in the declaration of the member.
|
Why the curly brackets, and not simply m_foo = 42?
|
|
|
10-23-2016, 03:59 AM
|
#5
|
Human being with feelings
Join Date: Oct 2007
Location: home is where the heart is
Posts: 12,110
|
Quote:
Originally Posted by Tale
Why the curly brackets, and not simply m_foo = 42?
|
To quote you
http://forum.cockos.com/showpost.php...70&postcount=4
So as I gather, the curly brackets seem to be similar to doing it in the constructor, but maybe Xen can clarify.
|
|
|
10-23-2016, 04:37 AM
|
#6
|
Human being with feelings
Join Date: May 2015
Location: Serbia
Posts: 654
|
Quote:
Originally Posted by Tale
Why the curly brackets, and not simply m_foo = 42?
|
Indeed, this is the easiest way. It works in cpp98 too.
|
|
|
10-23-2016, 06:39 AM
|
#7
|
Human being with feelings
Join Date: Feb 2007
Location: Oulu, Finland
Posts: 8,062
|
Quote:
Originally Posted by Tale
Why the curly brackets, and not simply m_foo = 42?
|
For things like ints and floating point numbers, using = works, but with more complicated types using the curly brackets is more convenient :
Code:
std::uniform_int_distribution<int> m_foo{ 0, 5 };
instead of
Code:
std::uniform_int_distribution<int> m_foo=std::uniform_int_distribution<int>(0,5);
The following does not work :
Code:
std::uniform_int_distribution<int> m_foo(0, 5);
__________________
I am no longer part of the REAPER community. Please don't contact me with any REAPER-related issues.
|
|
|
10-23-2016, 11:17 PM
|
#8
|
Human being with feelings
Join Date: Jul 2008
Location: The Netherlands
Posts: 3,653
|
Quote:
Originally Posted by nofish
|
Hehe... Yeah, but there I was assuming C++98, not C++11.
Quote:
Originally Posted by Xenakios
For things like ints and floating point numbers, using = works, but with more complicated types using the curly brackets is more convenient
|
Ah I see. Thanks!
|
|
|
Thread Tools |
|
Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -7. The time now is 08:54 AM.
|