Commented out function arguments?
Login to reply to this topic.
Tue, 2007-11-13 20:36
Joined: 2007-10-27
Forum posts: 8

Sorry for such a newbie question, but I can't seem to find a reference to the syntax of commenting out unused funtion arguments. I have never seen this before, and was wondering if it is a Symbian C++ syntax or a standard C++ syntax that I must have missed along the way...

Example:

void CMyAppView::Draw( const TRect& /*aRect*/ ) const

I guess what confuses me is that the whole argument is not commented out. The argument type is still declared, but not the argument variable name.

Also, after looking at tons of example Symbian C++ code, the use of the : and a code line after the function declaration, but before the body is something I have not seen before. Again, is this Symbian style or standard C++?

Example:

CMyAppUi::CMyAppUi(CMyDocument *aDoc)
:iEngine(NULL)
{
}

Isn't this just the same as putting the "iEngine(NULL)" inside the braces?

What is the advantage of using these two syntaxes?

I thought I knew C++ rather well, but after years of using C#, Java, and other languages while my C++ skills rusted, I am wondering if these are additions to the language since I learned it, or if it is a gap in my learning.

Thanks in advance for what seems to be a really newbie question... Hanging my head...


Wed, 2007-11-14 07:00
Joined: 2005-11-20
Forum posts: 1321

I am no C++ crack myself, but as as far as I know both things you mention are plain old standard C++.

Symbian does not rely on compiler and/or C++ language changes or extensions. Otherwise it would probably be impossible to use the Microsoft Visual C++ compiler for compiling programs for the Windows Symbian/UIQ emulator, as I am happily doing all the time...


René Brunner

Wed, 2007-11-14 07:01
Joined: 2007-09-03
Forum posts: 26

For the first part of your query:
These are called anonymous arguments. These provide a unique advantage in the sense that compiler can choose to ignore them when they are not specified.

And regarding 'CMyAppUi::CMyAppUi(CMyDocument *aDoc) : iEngine(NULL)',
This is called 'Base Initializer'. This is unique to C++. I dont know any specific advantage of it, but they say its a good programming practice.

Wed, 2007-11-14 08:59
Joined: 2003-12-05
Forum posts: 822

The argument in comments issue: you must specify the argument's data type, since otherwise the method would be completely different. In C++, methods (and functiona) can have same names as long as their parameters are different, and still they are completely different methods (functions).

Many times you need to implement a method inherited from a base class, but do not need some or all of the arguments. If you comment out the whole argument (with data type and all), this makes the function actually different - it is no longer overridden inherited method but something different, your own function just having the same name but different parameters. So just comment out the argument name to avoid warnings of unused arguments/parameters.

The second issue of initializers: in the constructor initialization list you can both call explicitly the base class' constructor and initialize the member variable values. If you do not add the base class constructor call, the compiler will do it for you and call the default constructor. In some cases, like when inheriting from CActive, there is no accessible default constructor and therefore, you need to explicitly call the CActive constructor with the priority parameter.

You can also initialize the values of the member variables. If the variable is static (not usual in Symbian) or const, then you have to use initialization lists. The option is to initialize the values in the actual body of the constructor:

CMyAppUi::CMyAppUi(CMyDocument *aDoc)
{
   iEngine = NULL;
}

However, using the initialization list is preferred, since it produces faster code. It also makes sure you initialize your member variables before calling any methods of the class in the body of the constructor, if this would be needed.

Thu, 2007-11-15 00:28
Joined: 2007-10-27
Forum posts: 8

Thanks all for the C++ review lesson! I had pretty much surmised what you all confirmed, but wanted to be sure.

rbrunner - Does the Carbide.vs plugin really use the Visual C++ compiler, or just the IDE and it calls the Symbian C++ compiler? I would think that since the ARM processor is what is the target for the Symbian and Visual C++ should be targeting the Intel-based chips, that the code generated by the Visual C++ compiler wouldn't work on a Symbian-based device. However, I haven't spent any time verifying this...

Andreas & aniait - Thanks for the names. It should help me find them in documentation (which was basically my problem). I can see the advantages of both, especially the Initializers. (Not so much the anonymous arguments, but I understand the reason why polymorphism requires the argument type, but allows for the commented out argument name). I will be able to use them with more confidence now.

I am actually enjoying remembering my C++ again.... Now if I could just really wrap my brain around the Symbian Leave and Stack structure... I get it, but just barely. Been leaning too much on C# and safe constructs, I guess.

Thu, 2007-11-15 06:56
Joined: 2005-11-20
Forum posts: 1321

rbrunner - Does the Carbide.vs plugin really use the Visual C++ compiler, or just the IDE and it calls the Symbian C++ compiler?

In general, the compiler used to produce code for the emulator running under Windows generates - naturally - x86 code, and the compiler used for the phone generates ARM code. As far as I know, over time, in the various SDKs, different Windows and different ARM (cross-)compilers were used. In my case - UIQ 2.1 - I use Visual C++ from command line for the emulator and the ARM-code-generating GCC that came with the UIQ SDK for the phone.


René Brunner


copyright 2003-2009 NewLC SARL