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

    casting pointer to template type

    Hello.

    I have another circular dependency issue, so was trying to handle by changing the variable access, to pointer type.

    But the pointer is for templated class.

    Basically i had
    Code:
    					OnePixel<float> fraster(pfVal);
    
    					RenderFuncParams rfp;
    					rfp.displayDataType = eDDT;
    					rfp.pInstance		= pInstance;
    					rfp.pfOut			= &fraster;
    when i got circular dependency issue. I tried the following
    float val = 0.0;
    OnePixel<float> * pfraster = &val;

    obviously gives error. But

    OnePixel<float> * pfraster = static_cast<(OnePixel<float> *)>(&val);

    even gives error " error C2681: 'float *': invalid expression type for dynamic_cast

    thanks a lot
    pdk
    Last edited by pdk5; January 15th, 2022 at 04:52 AM.

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

    Re: casting pointer to template type

    You can't use static_cast on pointers. Use reinterpret_cast.
    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.2.2)

  3. #3
    Join Date
    May 2015
    Posts
    468

    Re: casting pointer to template type

    Thankyou very much kaud !!!. Yes it compiled like magic

    I was some short break and now back to work and i feel like i have forgotten little bit c++ i knew..

    thanks a lot again.. i am sorry for asking basic qs..but your patience is always appreciated.

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

    Re: casting pointer to template type

    Note though, that this isn't consider 'good practice'. Usually reinterpret_cast highlights some design issue and such code is often considered 'flakey' and 'fragile'. Use at own risk!
    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.2.2)

  5. #5
    Join Date
    May 2015
    Posts
    468

    Re: casting pointer to template type

    Thanks a lot kaud. Yes, i will check with my collegues about this.

    But now, Im facing issue further on: somehow, the passed reinterpret cast pointer is not able to be written

    Actually, i am casting to abstract class pointer , is it a issue ?

    Code:
    Function()
    {
    		float val								= 0;
    		// TODO_PRIYA_FWA: Following line needs 3gRaster.h file to be included. This results in circular dependency
    		// So use the pointer type (by forward declaring), to avoid this issue.
    		//OnePixel<float>*		pfraster(pfVal);
    		SimOutputRaster<float> * pfraster		= reinterpret_cast<SimOutputRaster<float> *>(&val);
    		
    		
    		RenderFuncParams rfp;
    		rfp.displayDataType						= DISPLAYDATA_RSRP_5g;
    		rfp.pInstance							= pInstanceSim;
    		rfp.pfOut								= pfraster;
    		rfp.piOut								= nullptr;
    		rfp.numPixels							= 1;
    		rfp.SetUseMask(false);				
    		rfp.SetAssignColours(false);
    
    		rfp.ulStartPoint			= rFwaPoint.GetPixelIndex();
    		rfp.ulEndPoint				= rFwaPoint.GetPixelIndex() + 1;
    			
    		// Call RenderNewSimOutput, to get the required value at the FWA point
    		pOM->RenderNewSimOutput(rfp);
    		
    }
    
    bool OutputManager::RenderNewSimOutput(RenderFuncParams& rfp)
    {
    
    		const bool bSuccess = pSim->DoNewTerminalCalculation(i, pTermType, &params, rfp.displayDataType,
    															 bUseArraySettings, bIgnoreThresholds, inCellIndexWrtPixel, bSkipTrafficCheck,
    															 dOut, iOut, outInfo, outCs, pPos);
    
    		if ( rfp.pfOut ) (*rfp.pfOut)[iOutIndex] = float(dOut);
    		
    }
    I get the exception thrown at the red line.

    I tried to chekc the pointer value , is coming as 0x00000000006fe6c4, before passing to the function.

    At the exception, i tried to check, the pointer value is still the same .

    But

    Exception thrown : read access violation
    rfp.pfOut-> was 0xFFFFFFFFFFFFFFFF

    rfp.pfOut 0x00000000006fe6c4 {...}
    rfp.pfOut->__vfptr 0xcccccccc00000000 {???, ???, ???, ???, ???, ???, ???, ???}
    [0] = <Unable to read memory>


    Actually, i checked somewhere in other places where they pass pointer of type "OnePixel" instead of SimOutputRaster

    Actually the following is abstract class

    template <class T>
    class SimOutputRaster
    {
    public:
    virtual T& operator[](unsigned __int64 idx) = 0;

    //These have an implementation which won't be used
    virtual T GetIndexFor(int iCellKey, int iAntKey = 0, int iCarrierKey = 0) {return T(); };
    virtual void FillIndex(T /*iIndex*/, LPCTSTR /*pStr*/, COLORREF /*cr*/, int iCellSimIndex = 0, TgNetType netType = TGNETWORK_UNKNOWN, int iStaticEnumColourIndex = -1) { iCellSimIndex; };
    virtual T GetNumEntries() const {return T();};
    virtual void EndFillIndex() {};
    virtual int GetCellKey(T /*iIndex*/) const { return 0; };
    virtual int GetAntKey(T /*iIndex*/) const { return 0; };
    virtual int GetCarrierKey(T /*iIndex*/) const { return 0; };

    };

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

    Re: casting pointer to template type

    Simply, rfp.pfOut is a pointer to a float (val) which is a single variable and not an array. But you are treating rtp.pfOut as if it's an array pointer whn it's not. Hence the issue.

    reinterpret_cast<> is the surest way of shooting yourself in your left foot whilst pointing the gun at your right foot...
    Last edited by 2kaud; January 17th, 2022 at 05:51 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.2.2)

  7. #7
    Join Date
    May 2015
    Posts
    468

    Re: casting pointer to template type

    Thanks a lot kaud..

    Im seeing somewhere else in legacy code, (for getting int value)

    int iVal = 0;
    OnePixel<int> iraster(&iVal);

    RenderFuncParams rfp;
    rfp.displayDataType = eDDT;
    rfp.pInstance = pInstance;
    rfp.pfOut = nullptr;
    rfp.piOut = &iraster;

    where piOut is just single value, not an array

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

    Re: casting pointer to template type

    That just says that the pfOut pointer is set to nullptr - ie set to 0 ie not used/specified. Hence the test:

    Code:
    if ( rfp.pfOut )
    which checks for the nullptr condition.
    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.2.2)

  9. #9
    Join Date
    May 2015
    Posts
    468

    Re: casting pointer to template type

    i.e true, because output can be int or float..

    if out is int, rfp.piOut is used
    if float rfp.pfOut is used

    in our case it is float..(not array)

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

    Re: casting pointer to template type

    in our case it is float..(not array)
    ...but you're indexing rfp.pfOut via iOutIndex as if it's an array...
    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.2.2)

  11. #11
    Join Date
    May 2015
    Posts
    468

    Re: casting pointer to template type

    thanks a lot kaud. But the function im calling (RenderNewSimOutput) is legacy

    in my case, iOutIndex will just be "0", as its just one pixel

    Code:
    bool OutputManager::RenderNewSimOutput(RenderFuncParams& rfp)
    {
    	for (INT_PTR i = 0, iOutIndex = 0, iProgressIndex = rfp.ulStartPoint;
    		iProgressIndex < rfp.ulEndPoint;	//End condition 
    		iProgressIndex++)					//Increment operation
    
    {
    		const bool bSuccess = pSim->DoNewTerminalCalculation(i, pTermType, &params, rfp.displayDataType,
    															 bUseArraySettings, bIgnoreThresholds, inCellIndexWrtPixel, bSkipTrafficCheck,
    															 dOut, iOut, outInfo, outCs, pPos);
    
    		if ( rfp.pfOut ) (*rfp.pfOut)[iOutIndex] = float(dOut);
    		
    }

  12. #12
    Join Date
    May 2015
    Posts
    468

    Re: casting pointer to template type

    Thanks a lot kaud, there was function i could call to get the output array. And that i passed and now it works ok !!.

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