Go to Post While at kickoff I asked Woodie if there were any concerns about missed shots at the center goal hitting the operators or the controls. He handed me a ball and told me to throw it at him as hard as I could... How many times does an opportunity like that come up? - Barry Bonzack [more]
Home
Go Back   Chief Delphi > Technical > Programming > C/C++
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 17-10-2013, 22:18
adlasa's Avatar
adlasa adlasa is offline
President and Lead Programmer
AKA: Sasha
FRC #1458 (Red Ties)
Team Role: Leadership
 
Join Date: Dec 2012
Rookie Year: 2011
Location: Bay Area
Posts: 35
adlasa has a spectacular aura aboutadlasa has a spectacular aura aboutadlasa has a spectacular aura about
Same initialization as declaration in constructor, why?

I was looking through the basic tutorial on C++ with an FRC robot.

The code:

Code:
public:
   RobotDemo(void):
      myRobot(1,2),
      stick(1)
The comments say these declarations have to be initialized in the same order as they are declared.

Why is this?
__________________
Computers run on smoke. When the smoke escapes the computers stop working.

Last edited by adlasa : 17-10-2013 at 23:01.
Reply With Quote
  #2   Spotlight this post!  
Unread 19-10-2013, 11:56
RyanCahoon's Avatar
RyanCahoon RyanCahoon is offline
Disassembling my prior presumptions
FRC #0766 (M-A Bears)
Team Role: Engineer
 
Join Date: Dec 2007
Rookie Year: 2007
Location: Mountain View
Posts: 689
RyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond repute
Re: Same initialization as declaration in constructor, why?

I don't know of anything in the C++ standard that requires the member initialization list to be in declaration order. It might be some limitation of the compiler, or they might be encouraging that for code readability because the C++ standard does say that no matter the order in the initialization list, the class members will be initialized in the order they are declared in the class definition.

I don't have windriver installed so I can't check to be sure which it might be, but there's nothing keeping you from trying it out. What happens if you reverse the initializers in the example code and try to compile it?
__________________
FRC 2046, 2007-2008, Student member
FRC 1708, 2009-2012, College mentor; 2013-2014, Mentor
FRC 766, 2015-, Mentor
Reply With Quote
  #3   Spotlight this post!  
Unread 21-10-2013, 10:00
nightpool's Avatar
nightpool nightpool is offline
robotRectifier
AKA: Evan
no team (formerly of CORE 2062)
Team Role: Alumni
 
Join Date: Oct 2011
Rookie Year: 2011
Location: Waukesha, WI
Posts: 81
nightpool is on a distinguished road
Re: Same initialization as declaration in constructor, why?

Its not a requirement, but strongly recommended, as giving them a different order will lead to very unintuitive behavior, as well as a warning from GCC. As Ryan said, no matter what you put in the initialization list, the members'll be initialized in the order they are declared up above.
__________________
Proud alum of CORE 2062.
www.core2062.com
Reply With Quote
  #4   Spotlight this post!  
Unread 24-10-2013, 00:38
William Kunkel William Kunkel is offline
Programming Lead
AKA: Kunkel
FRC #0422 (Mech Tech Dragons)
Team Role: Programmer
 
Join Date: Jan 2013
Rookie Year: 2011
Location: Richmond, VA
Posts: 94
William Kunkel is an unknown quantity at this point
Re: Same initialization as declaration in constructor, why?

The reason for this is that if you want to declare a member variable as some transformation of another member variable, you can end up with declaring it before the first variable is initialized. So, if you have:
Code:
class Example {
private:
    int a;
    int b;
public:
    Example();
};

Example::Example():
    b(2), a(b+1) {
}
the value of a will not be 3, but undefined, even though it looks like it's declared after b. It's not a compiler error to have them in a different order, but it will produce a warning (I believe the warning is "warning: when initialized here").
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 14:38.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi