CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    May 2015
    Posts
    474

    passing string as default argument (reference)

    Hello,

    Code:
    double GetEffAzimuth(const MultiTechCell* pCell, std::string& error)
    {
    
    	const auto& activeAntennas = pCell->GetActiveAntennas();
    
    	const ActiveAntenna*	pActAntenna = activeAntennas.empty() ? nullptr : activeAntennas.front();	ASSERT_ONCE(pActAntenna);
    	const LogicalAntenna*	pLogAntenna = pActAntenna ? pActAntenna->GetLogicalAntenna() : nullptr;		ASSERT_ONCE(pLogAntenna);
    
    	const AntennaType* pAntType			= pLogAntenna->GetAntennaType();								ASSERT_ONCE(pAntType);
    	const bool bSwitchedBeam			= pAntType->GetParentDevice().IsSwitchedBeamType();
    
    	double dAzimuthDegreesA				= (bSwitchedBeam) ? pLogAntenna->GetAzimuth() : pLogAntenna->GetEffectiveAzimuth();;
    
    	// Map azimuth to range 0 to 360
    	while (dAzimuthDegreesA < 0) dAzimuthDegreesA += 360.0;
    	while (dAzimuthDegreesA >= 360.0) dAzimuthDegreesA -= 360.0;
    
    	// Check if, omni directional antenna 
    	const bool bOmni = (bSwitchedBeam) ? false : (pAntType->GetHorizontalBW() == 360.0);
    
    	if (bOmni) dAzimuthDegreesA = -1.0;	// A hack.  Using -ve azimuth to signify we have an omni.
    
    	return dAzimuthDegreesA;
    }
    The above function is already being called in some places in current code.

    Now to add some more logging info, I need to pass the error string into this function as default arguement, so the existing calls wont get affected.

    Code:
    double GetEffAzimuth(const MultiTechCell* pCell, std::string& error = "")
    {
    
    	const auto& activeAntennas = pCell->GetActiveAntennas();
    
    	const ActiveAntenna*	pActAntenna = activeAntennas.empty() ? nullptr : activeAntennas.front();
            error = "No Active antenna";
    	ASSERT_ONCE(pActAntenna);
    	const LogicalAntenna*	pLogAntenna = pActAntenna ? pActAntenna->GetLogicalAntenna() : nullptr;	
            error = "No Logical antenna";
    	ASSERT_ONCE(pLogAntenna);
    
    	const AntennaType* pAntType			= pLogAntenna->GetAntennaType();	
            error = "No Antenna type";
    							ASSERT_ONCE(pAntType);
    	const bool bSwitchedBeam			= pAntType->GetParentDevice().IsSwitchedBeamType();
    
    	double dAzimuthDegreesA				= (bSwitchedBeam) ? pLogAntenna->GetAzimuth() : pLogAntenna->GetEffectiveAzimuth();;
    
    	// Map azimuth to range 0 to 360
    	while (dAzimuthDegreesA < 0) dAzimuthDegreesA += 360.0;
    	while (dAzimuthDegreesA >= 360.0) dAzimuthDegreesA -= 360.0;
    
    	// Check if, omni directional antenna 
    	const bool bOmni = (bSwitchedBeam) ? false : (pAntType->GetHorizontalBW() == 360.0);
    
    	if (bOmni) dAzimuthDegreesA = -1.0;	// A hack.  Using -ve azimuth to signify we have an omni.
    
    	return dAzimuthDegreesA;
    }
    But i am unable to pass NULL into default argument. Sorry i didnot do the homework, as it is urgent. so will be very helpful , if you can comment and let me know if any alternatives

    thanks
    pdk

  2. #2
    Join Date
    May 2015
    Posts
    474

    Re: passing string as default argument (reference)

    This is some last minute request and i have other things to looks, so sorry for not doing home work.
    Also this is urgent, So kaud, could you kindly help me please ?

  3. #3
    Join Date
    May 2015
    Posts
    474

    Re: passing string as default argument (reference)

    I changed the following code works. (without default arguement)
    But then i change all places, the function is currently used

    Will be helpul, if there is better way of doing this ?

    Code:
    double GetEffAzimuth(const MultiTechCell* pCell, std::string & s2)
    {
    	double dAzimuthDegreesA = -2; // Indicate that cells cannot be paired due to errors
    
    	const auto& activeAntennas = pCell->GetActiveAntennas();
    
    	const ActiveAntenna*	pActAntenna = activeAntennas.empty() ? nullptr : activeAntennas.front();	
    	if (pActAntenna == nullptr)
    	{
    		s2 = "No active antenna";
    		return dAzimuthDegreesA;
    	}
    
    	const LogicalAntenna*	pLogAntenna = pActAntenna ? pActAntenna->GetLogicalAntenna() : nullptr;		
    	if (pActAntenna == nullptr)
    	{
    		s2 = "No logical antenna";
    		return dAzimuthDegreesA;
    	}
    
    	const AntennaType* pAntType			= pLogAntenna->GetAntennaType();	
    	if (pAntType == nullptr)
    	{
    		s2 = "No Antenna type";
    		return dAzimuthDegreesA;
    	}
    
    	const bool bSwitchedBeam			= pAntType->GetParentDevice().IsSwitchedBeamType();
    
    	dAzimuthDegreesA					= (bSwitchedBeam) ? pLogAntenna->GetAzimuth() : pLogAntenna->GetEffectiveAzimuth();
    
    	// Map azimuth to range 0 to 360
    	while (dAzimuthDegreesA < 0) dAzimuthDegreesA += 360.0;
    	while (dAzimuthDegreesA >= 360.0) dAzimuthDegreesA -= 360.0;
    
    	// Check if, omni directional antenna 
    	const bool bOmni = (bSwitchedBeam) ? false : (pAntType->GetHorizontalBW() == 360.0);
    
    	if (bOmni) dAzimuthDegreesA = -1.0;	// A hack.  Using -ve azimuth to signify we have an omni.
    
    	return dAzimuthDegreesA;
    }

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,493

    Re: passing string as default argument (reference)

    Pass it by value, not by reference.

  5. #5
    Join Date
    Feb 2017
    Posts
    661

    Re: passing string as default argument (reference)

    Quote Originally Posted by pdk5 View Post
    if there is better way of doing this ?
    You could return an std:: pair (or std::tuple). The function signature would be,
    Code:
    std::pair<double, std::string> GetEffAzimuth(const MultiTechCell* pCell)
    Return statements would look like this,
    Code:
    return std::makepair(dAzimuthDegreesA, ""); // no error
    //
    return std::makepair(dAzimuthDegreesA, "No logical antenna");  // some error
    There are variations to this and also std:: option could be used to indicate that dAzimuthDegreesA is in an erroneous state. But my main suggestion is that you utilize that C++ allows for multiple returns. Personally I prefer this over output parameters when applicable.
    Last edited by wolle; August 19th, 2020 at 11:37 PM.

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,661

    Re: passing string as default argument (reference)

    As error is a return parameter, it can't be passed by value - only by ref. Also, as it's a ref you can't provide a default value. If you want to have the option to specify this parameter or not (so that existing code can compile without supplying the extra argument), then this can be done by function overload. Consider:

    Code:
    double GetEffAzimuth(const MultiTechCell* pCell)
    {
        std::string sink;
    
        return GetEffAzimuth(pCell, sink);
    }
    
    double GetEffAzimuth(const MultiTechCell* pCell, std::string& error)
    {
        //........
    }
    Last edited by 2kaud; August 20th, 2020 at 02:29 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.3.4)

  7. #7
    Join Date
    May 2015
    Posts
    474

    Re: passing string as default argument (reference)

    Thanks a lot, kaud for the explanation. I was trying to see if i can provide default value as reference yesterday and was not possible. Thankyou very much for making me understand and confirminmg that . Very helpful , and thankyou for the quick response.

    As of now i have gone with option of changing it in legacy code to pass error, as it was some couple of places. but in future the suggestion to go for function overloading will help, in case of changes in lot of places.

  8. #8
    Join Date
    May 2015
    Posts
    474

    Re: passing string as default argument (reference)

    Another minor related question:
    Is it better to do:
    const ActiveAntenna* pActAntenna = activeAntennas.empty() ? nullptr : activeAntennas.front();
    if (!pActAntenna)
    {
    sError = "No active antenna";
    return dAzimuthDegreesA;
    }

    Or
    const ActiveAntenna* pActAntenna = activeAntennas.empty() ? nullptr : activeAntennas.front();
    if (pActAntenna == nullptr)
    {
    sError = "No active antenna";
    return dAzimuthDegreesA;
    }

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,661

    Re: passing string as default argument (reference)

    Both will work - but good practice in C++ is to use nullptr.

    But why introduce nullptr? Consider:

    Code:
    if (activeAntennas.empty())
    {
        sError = "No active antenna";
        return dAzimuthDegreesA;
    }
    
    const ActiveAntenna* pActAntenna = activeAntennas.front(); 
    
    //...
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.3.4)

  10. #10
    Join Date
    May 2015
    Posts
    474

    Re: passing string as default argument (reference)

    Thanks a lot kaud for the info. And also for suggesting the better code . very helpful .

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured