[ 
https://issues.apache.org/jira/browse/XALANC-733?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13464742#comment-13464742
 ] 

Martin Elzen commented on XALANC-733:
-------------------------------------

Okay, well, IBM is telling you a part of the story... but not all of it.  They 
failed to mention that the "const int Test::N;" bit needs to be in a .cpp file. 
 If you put in in the header instead you mess up what IIRC they call C++'s 
seperate compilation model.  If you then happen to include the same header file 
into 2 different translation units, those two translation units can't be 
combined since they contain a duplicate definition.  

Here's a "mored-together" file (if you want to use it esp. the makefile you 
will need to put the tabs where they belong!)

::::::::::::::
test.h
::::::::::::::
#if !defined(TEST_HDR)
   #define TEST_HDR

   
        class Test
        { 
        public:
                static const int N = 10;

                void sayHi() const;
        };


        #if defined(BAD)
        const int Test::N;
        #endif
        

#endif
::::::::::::::
test.cpp
::::::::::::::
#include "test.h"

#include <iostream>



#if defined(GOOD)
const int Test::N;
#endif
        
   

void Test::sayHi() const
{ 
        std::cout << "Test says 'Hi'.\n";
}



::::::::::::::
testUserOne.h
::::::::::::::


class UserOne {

public:

        void sayHi() const;

};
::::::::::::::
testUserTwo.h
::::::::::::::


class UserTwo {

public:

        void sayHi() const;

};
::::::::::::::
testUserOne.cpp
::::::::::::::

#include "test.h"
#include "testUserOne.h"

#include <iostream>




void UserOne::sayHi() const
{
        std::cout << "UserOne, Test::N == " << Test::N << "\n";
}


::::::::::::::
testUserTwo.cpp
::::::::::::::

#include "test.h"
#include "testUserTwo.h"

#include <iostream>




void UserTwo::sayHi() const
{
        std::cout << "UserTwo, Test::N == " << Test::N << "\n";
}


::::::::::::::
main.cpp
::::::::::::::

#include "test.h"
#include "testUserOne.h"
#include "testUserTwo.h"

#include <iostream>



int main(int argc, const char *argv[] )
{
        std::cout << "at start of main\n";

        
        Test      tst;
        UserOne   one;
        UserTwo   two;
        
        tst.sayHi();
        one.sayHi();
        two.sayHi();

        
        std::cout << "at end of main\n";
        return 0;
}


::::::::::::::
makefile
::::::::::::::
CC=g++

#OPTIONS=-DBAD 
OPTIONS=-DGOOD


clean:
        rm main main.o test.o testUserOne.o testUserTwo.o


test.o: test.h test.cpp
        $(CC) $(OPTIONS) -o test.o -c test.cpp



testUserOne.o: test.h testUserOne.h testUserOne.cpp
        $(CC) $(OPTIONS) -o testUserOne.o -c testUserOne.cpp



testUserTwo.o: test.h testUserTwo.h testUserTwo.cpp
        $(CC) $(OPTIONS) -o testUserTwo.o -c testUserTwo.cpp



main.o: main.cpp 
        $(CC) $(OPTIONS) -o main.o -c main.cpp 



main: test.o testUserOne.o testUserTwo.o main.o 
        $(CC) $(OPTIONS) -o main test.o testUserOne.o testUserTwo.o main.o 



#####

GNU g++ (4.4) won't compile this when the BAD define in the Makefile isn't 
commented-out, while it will compile and link just fine when the GOOD define 
isn't commented out.


That's why I'm pushing for an approach using a static inline member function 
that returns a reference to a static object that is constructed within said 
static function.  First of all, this is a well-known idiom (it's the singelton 
design pattern).  You are also making the language work for you: doing it like 
this makes it C++'s responsibility to create the object exactly once.  And as a 
plus point, this kind of design will even be more thread-safe when C++ 
compilers more completely support the C++11 standard.  











                
> Ensure that XalanLocator::getSystemId() and getPublicId() do not return NULL
> ----------------------------------------------------------------------------
>
>                 Key: XALANC-733
>                 URL: https://issues.apache.org/jira/browse/XALANC-733
>             Project: XalanC
>          Issue Type: Bug
>          Components: XalanC, XPathC
>    Affects Versions: 1.11
>            Reporter: Steven J. Hathaway
>            Assignee: Steven J. Hathaway
>         Attachments: XalanLocator-3.patch, XalanLocator.patch, 
> XalanLocator.patch2
>
>
> The recommended patch to "xalanc/PlatformSupport/XalanLocator.hpp"
> ensures that getSystemId() and getPublicId() do not return NULL pointers.
> XalanC source files that can benefit from the patch include:
> xalanc/PlatformSupport/ProblemListenerBase.cpp
> xalanc/PlatformSupport/XSLException.cpp
> xalanc/XPath/XPathExecutionContextDefault.cpp
> The patch will be submitted shortly.

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to