Re: Templates and access checking

From: Jari Kalevi Savolainen (jaksavol_at_hytti.uku.fi)
Date: 01/04/05


Date: Tue, 4 Jan 2005 17:55:31 +0200

On Tue, 4 Jan 2005, Victor Bazarov wrote:

> Several.
>
> First, why did you reply to another, unrelated, thread instead of starting
> a new one? Not a good idea.

That was my honest intention, mistake on my part.

>
> Second, I am not sure what you expect. c_store<A,A> and c_store<A,B> are
> two distinct and unrelated types. Unless you tell them to be friends of
> each other, there is no seeing of any privates.

Yes.

>
> Third, if you really need a design suggestion, post the real problem you
> are trying to solve instead of a hypothetical, contrived, and non-working
> solution.
>

I tried to provide a small compact code sample. Too small maybe and the
copy constructor I chose because I need to deep copy dynamically allocated
memory.

> Now, there are several work-arounds. A friend member function would be
> one. You can make them (instantiations) all friends of each other. You
> can have a member function that simply returns a value of 'data'. I am
> not sure what "exposure" you're afraid of.

Will try. Not sure if I've tried this.

The 'real' problem involves ptrs/dynamic memory. Keeping the pointer
private is my goal.

> A common base class probably
> won't work since an instance is not allowed to see even protected members
> of an object of another derived type.

It can be done but you need to bounce pointers around.
Not going to get any deeper into how, but it works.
But it creates the same problem as methods returning ptr/value need to be
public.

> Another solution is to drop the
> 'multiType' from the main template and _always_ have it for members:
>
> template<class T> class c_store {
> T data;
> public:
> template<class mT> c_store(mT md = 3) : data(md * 3) {}
> c_store(c_store const & cs) : data(cs.data) {} // actually no need
> };
>
> That of course may not be acceptable if you do need distinct types like
> c_store<int,int> and c_store<int,double>. But I cannot conclude that from
> the example you gave, that's why you need to state the real problem.
>

Again I obviously provided bad code sample, in my 'real' project it really
makes a difference and solution:
> template<class mT> c_store(mT md = 3) : data(md * 3) {}
Could result in unexpected behauvior, of course this is one way to do it
but it could easily lead to a situation where template generates code that
compiles fine but results are unexpected because the types are unsuitable
for operations performed.

> Please elaborate why these work-arounds are not suitable in your case.
>
> As to whether there was a discussion, try groups.google.com, that's a very
> good source of information that has been discussed. Yes, you'd need to
> dig through some posts. But that's what the search is for. Computer
> memory is much more reliable than human, keep that in mind.

Did that but couldn't find anything specific to this.

>
> Victor
>



Relevant Pages

  • RE: USB Hard Drive Problems
    ... If you have the chance I would suggest building a custom kernel with the ... The only real problem ... >If I plug my memory stick into the USB port, ...
    (freebsd-questions)
  • Re: Save an array to memory for another program to read it
    ... I need the program to store into memory this array, end itself and when immediately runs again to load this array again from the memory. ... have said "subroutine" so what it required is to read a bit further into ... It is the old question of whether the stated problem is the real problem ...
    (comp.lang.fortran)
  • Re: "Disk Image" alike for Pocket PC Mobile 5 - Installation of big amount of devices
    ... isn't true - it even changes over hard resets and can freely be loaded. ... software that is specific for USB host capable PPCs and Phone Edition ... image that simply loads too much into memory for a less memory-capable ... I agree that it is not a real problem, it just may not be the results ...
    (microsoft.public.pocketpc)
  • Re: insufficient memory OE6
    ... The short of memory / disk space error is a sort of drop through catch all ... that doesn't usually point to the real problem. ... > another error pops up saying there is insufficient memory ...
    (microsoft.public.windows.inetexplorer.ie6_outlookexpress)