COCKOS
CONFEDERATED FORUMS
Cockos : REAPER : NINJAM : Forums
Forum Home : Register : FAQ : Members List : Search :

Go Back   Cockos Incorporated Forums > Other Software Discussion > WDL users forum

Reply
 
Thread Tools Display Modes
Old 10-22-2016, 06:01 AM   #1
nofish
Human being with feelings
 
nofish's Avatar
 
Join Date: Oct 2007
Location: home is where the heart is
Posts: 12,110
Default initializing members during construction

Does it make a difference if I init member variables via initialization list or in the body of the constructor ?
nofish is offline   Reply With Quote
Old 10-22-2016, 10:07 AM   #2
Xenakios
Human being with feelings
 
Xenakios's Avatar
 
Join Date: Feb 2007
Location: Oulu, Finland
Posts: 8,062
Default

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.
Xenakios is offline   Reply With Quote
Old 10-22-2016, 12:46 PM   #3
nofish
Human being with feelings
 
nofish's Avatar
 
Join Date: Oct 2007
Location: home is where the heart is
Posts: 12,110
Default

Thanks Xenakios.
The C++11 method is new to me.
nofish is offline   Reply With Quote
Old 10-23-2016, 02:50 AM   #4
Tale
Human being with feelings
 
Tale's Avatar
 
Join Date: Jul 2008
Location: The Netherlands
Posts: 3,653
Default

Quote:
Originally Posted by Xenakios View Post
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?
Tale is offline   Reply With Quote
Old 10-23-2016, 03:59 AM   #5
nofish
Human being with feelings
 
nofish's Avatar
 
Join Date: Oct 2007
Location: home is where the heart is
Posts: 12,110
Default

Quote:
Originally Posted by Tale View Post
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.
nofish is offline   Reply With Quote
Old 10-23-2016, 04:37 AM   #6
Youlean
Human being with feelings
 
Youlean's Avatar
 
Join Date: May 2015
Location: Serbia
Posts: 654
Default

Quote:
Originally Posted by Tale View Post
Why the curly brackets, and not simply m_foo = 42?
Indeed, this is the easiest way. It works in cpp98 too.
Youlean is offline   Reply With Quote
Old 10-23-2016, 06:39 AM   #7
Xenakios
Human being with feelings
 
Xenakios's Avatar
 
Join Date: Feb 2007
Location: Oulu, Finland
Posts: 8,062
Default

Quote:
Originally Posted by Tale View Post
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.
Xenakios is offline   Reply With Quote
Old 10-23-2016, 11:17 PM   #8
Tale
Human being with feelings
 
Tale's Avatar
 
Join Date: Jul 2008
Location: The Netherlands
Posts: 3,653
Default

Quote:
Originally Posted by nofish View Post
Hehe... Yeah, but there I was assuming C++98, not C++11.

Quote:
Originally Posted by Xenakios View Post
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!
Tale is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 08:54 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.