POV-Ray : Newsgroups : povray.off-topic : Adventures with C++ : Re: An actual C++ question Server Time
29 Jul 2024 02:30:46 EDT (-0400)
  Re: An actual C++ question  
From: Warp
Date: 16 May 2013 14:57:33
Message: <51952c1d@news.povray.org>
Le_Forgeron <jgr### [at] freefr> wrote:
> You can either have all methods of a class static, or use a singleton
> pattern.

If you are using an enforced singleton, and you don't want to expose
*any* implementation detail to the outside, one possibility would be
to do this:

//------------------------------------------------------------
// In the header file:
// ------------------

class TheClass
{
 public:
    virtual void someFunction() = 0;
    virtual int anotherFunction(int parameter) = 0;
    virtual void aThirdFunction(double, double) = 0;
};

TheClass& theClassInstance();
//------------------------------------------------------------

//------------------------------------------------------------
// In the implementation file:
// --------------------------

#include "TheClass.hh"

namespace
{
    class TheActualClass: public TheClass
    {
        struct InnerType { int a, b; }

        int privateMember;
        double anotherPrivateMember;
        InnerType aThirdPrivateMember;

        void privateFunction();

     public:
        TheActualClass() { /* constructor */ }

        virtual void someFunction()
        {
            // implement it here
        }

        virtual int anotherFunction(int parameter)
        {
            // implement it here
        }

        virtual void aThirdFunction(double, double)
        {
            // implement it here
        }
    };
}

TheClass& theClassInstance()
{
    static TheActualClass actualClassInstance;
    return actualClassInstance;
}
//------------------------------------------------------------

-- 
                                                          - Warp


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.