POV-Ray : Newsgroups : povray.off-topic : Adventures with C++ Server Time
29 Jul 2024 06:26:37 EDT (-0400)
  Adventures with C++ (Message 16 to 25 of 65)  
<<< Previous 10 Messages Goto Latest 10 Messages Next 10 Messages >>>
From: Warp
Subject: Re: An actual C++ question
Date: 15 May 2013 17:53:17
Message: <519403cd@news.povray.org>
Warp <war### [at] tagpovrayorg> wrote:
>     class PrivateClass;

>     class MyClass
>     {
>         std::shared_ptr<PrivateClass> privateObject;
>     };

Oh, and take into account that if instances of MyClass are copied around,
the copies will share the one and same PrivateClass object. That will not
get deep-copied. (That's what the "shared ptr" means, actually.)

If you want to avoid MyClass being copied around, that can be disabled.
(Of course this restricts the ways in which the class can be used, so
it's always a compromise.)

Another possibility is to write a copy constructor and a copy assignment
operator that deep-copies the private object... It all depends on how
exactly you need MyClass to behave.

-- 
                                                          - Warp


Post a reply to this message

From: Orchid Win7 v1
Subject: Re: An actual C++ question
Date: 16 May 2013 03:39:25
Message: <51948d2d$1@news.povray.org>
>>      class PrivateClass;
>
>>      class MyClass
>>      {
>>          std::shared_ptr<PrivateClass>  privateObject;
>>      };

OK. That seems easy enough.

> Oh, and take into account that if instances of MyClass are copied around,
> the copies will share the one and same PrivateClass object. That will not
> get deep-copied. (That's what the "shared ptr" means, actually.)

There "should" only ever be one instance of this class; I'd just like to 
avoid the program exploding if somebody *does* copy that instance for 
some reason. A shallow copy is acceptable.


Post a reply to this message

From: scott
Subject: Re: An actual C++ question
Date: 16 May 2013 10:55:52
Message: <5194f378$1@news.povray.org>
> There "should" only ever be one instance of this class; I'd just like to
> avoid the program exploding if somebody *does* copy that instance for
> some reason. A shallow copy is acceptable.

Is there a C++ equivalent to the C# static class? That would ensure at 
compile-time that nobody tried to create an(other) instance.


Post a reply to this message

From: Warp
Subject: Re: An actual C++ question
Date: 16 May 2013 11:27:04
Message: <5194fac8@news.povray.org>
scott <sco### [at] scottcom> wrote:
> > There "should" only ever be one instance of this class; I'd just like to
> > avoid the program exploding if somebody *does* copy that instance for
> > some reason. A shallow copy is acceptable.

> Is there a C++ equivalent to the C# static class? That would ensure at 
> compile-time that nobody tried to create an(other) instance.

You could use a namespace instead of a class. Private implementation can
then all go to the source file, away from the header file.

On the other hand, this limits where you can use this "pseudoclass" and
it could be difficult to maintain backwards compatibility if you ever
wanted to change it so that more than one instance could be created.

Of course you could have a class that only has static member functions...

-- 
                                                          - Warp


Post a reply to this message

From: Le Forgeron
Subject: Re: An actual C++ question
Date: 16 May 2013 13:19:26
Message: <5195151e$1@news.povray.org>
Le 16/05/2013 17:27, Warp nous fit lire :
> scott <sco### [at] scottcom> wrote:

>> Is there a C++ equivalent to the C# static class? That would ensure at 
>> compile-time that nobody tried to create an(other) instance.
> 

You can either have all methods of a class static, or use a singleton
pattern.

> You could use a namespace instead of a class. Private implementation can
> then all go to the source file, away from the header file.
> 
> On the other hand, this limits where you can use this "pseudoclass" and
> it could be difficult to maintain backwards compatibility if you ever
> wanted to change it so that more than one instance could be created.
> 
> Of course you could have a class that only has static member functions...
>


Post a reply to this message

From: Orchid Win7 v1
Subject: Re: Adventures with C++
Date: 16 May 2013 13:46:20
Message: <51951b6c@news.povray.org>
On 14/05/2013 06:29 PM, Orchid Win7 v1 wrote:
> But that's nothing compared to how long it took to get VS to produce a
> DLL, and then get C# to successfully call this DLL without access
> violations... >_<

But THAT is nothing compared to the trouble we had trying to build a 
Linux "shared object" and getting Mono to load that...

I can still hardly believe that this whole improbable contraption 
actually ended up WORKING in the end! o_O


Post a reply to this message

From: Warp
Subject: Re: An actual C++ question
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

From: Orchid Win7 v1
Subject: Re: An actual C++ question
Date: 17 May 2013 13:20:28
Message: <519666dc$1@news.povray.org>
On 16/05/2013 08:39 AM, Orchid Win7 v1 wrote:
>>> class PrivateClass;
>>
>>> class MyClass
>>> {
>>> std::shared_ptr<PrivateClass> privateObject;
>>> };
>
> OK. That seems easy enough.

I entered this into VisualStudio, and it worked great.

However, when I tried to compile it under Linux, GCC refused to accept it.

First, VS seems to somehow allow you to compile things even without 
actually importing the necessary include files. (E.g., it will happily 
accept std::shared_ptr even though I didn't #include <memory>.) GCC 
demands that you actually specify the include files [like you'd expect]. 
Weird, but true.

Second, it seems this "feature" only exists in C++11. I don't know what 
GCC is defaulting to, but apparently you have to add a switch to tell it 
to accept C++11. And when I do that, my code compiles perfectly, but the 
rest of the codebase breaks spectacularly.

So, in summary, I had to revert everything back to how it was. *sigh*


Post a reply to this message

From: Orchid Win7 v1
Subject: Re: Adventures with C++
Date: 17 May 2013 13:22:33
Message: <51966759$1@news.povray.org>
On 16/05/2013 06:46 PM, Orchid Win7 v1 wrote:
> I can still hardly believe that this whole improbable contraption
> actually ended up WORKING in the end! o_O

The daft thing is, it turns out to actually be pretty easy to write some 
C++, make a DLL out of it, and call that from C#. The actual code that 
ended up in source control is both small and simple.

This belies the vast amount of man-hours it took to *arrive* at this 
final arrangement. It took an absurd amount of Google searches and 
trail-and-error to get the myriad of pieces to fit together just right.

In summary, it's easy when you know how. So easy, presumably, that 
nobody bothers to write down how it's actually done - so the next person 
who tries to do this will have the exact same problem. :-/


Post a reply to this message

From: Warp
Subject: Re: An actual C++ question
Date: 17 May 2013 14:16:01
Message: <519673e1@news.povray.org>
Orchid Win7 v1 <voi### [at] devnull> wrote:
> Second, it seems this "feature" only exists in C++11. I don't know what 
> GCC is defaulting to, but apparently you have to add a switch to tell it 
> to accept C++11. And when I do that, my code compiles perfectly, but the 
> rest of the codebase breaks spectacularly.

> So, in summary, I had to revert everything back to how it was. *sigh*

You could use std::auto_ptr instead of std::shared_ptr, but then you'd
better disable copying for your class.

-- 
                                                          - Warp


Post a reply to this message

<<< Previous 10 Messages Goto Latest 10 Messages Next 10 Messages >>>

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