Private/protected inheritance problem

From: Mark A. Gibbs (x_gibbsmark_at_rogers.com_x)
Date: 03/12/05


Date: Fri, 11 Mar 2005 22:26:13 -0500


Good day,

i'm having a bit of trouble with a base class i'm working on. this is
what it boils down to:

template <typename T>
class foo
{
protected:
    foo() { T* p = static_cast<T*>(this); }
};

class bar : foo<bar>
{
public:
    bar() {}
};

as is bar inherits privately from foo, and i get a compile error on the
assignment saying "Illegal access from bar to private/protected member
foo::" (that's exactly what it says, just "foo::").

when i use public inheritance, there's no problem. however, i get the
same problem for protected inheritance, or when i make foo::foo() a
public constructor.

is there a reason for this? i mean, when foo::foo() is instantiated, the
relationship between foo and T is known and clear. i don't see what
private member of foo bar is trying to access. i tried making a
workaround by doing this:

int distance = static_cast<foo<T>*>((T*)0);
T* p = reinterpret_cast<T*>(this - distance);

but it still failed on the first line.

i'm using codewarrior 8. what i want to do is get a pointer to the
derived class from the base class.

-- 
Mark A. Gibbs (aka. Indi)
Administrator
#c++ on irc.Rizon.net
http://ca.geocities.com/indij@rogers.com/
(temporary website)


Relevant Pages

  • Difficulty Extending from Generic Base Class
    ... Bar extend from BaseClass): ... public class Foo: BaseClass ... public override string ToString() ... The list of abstract base class items is a concrete class. ...
    (microsoft.public.dotnet.framework.clr)
  • Re: Insert with response
    ... FooBar, there's no way and no need to put them in synch. ... column in the foo table to 250 calumns in the bar table. ... set statistics time off ...
    (microsoft.public.sqlserver.programming)
  • Re: Magic function
    ... processing objects created in root at depth 3 ... root obj2 at depth 3 ... processing objects created in foo at depth 2 ... processing objects created in bar at depth 0 ...
    (comp.lang.python)
  • Re: Any Clojure users here?
    ... (let [my-list (foo x)] ... If the stack depth is N when you call baz, it's only N+3 in bar and N+2 ... It is N+3 in map, but map creates a lazy sequence object rather than ... a Java method call. ...
    (comp.lang.lisp)
  • Re: Closing parenthesis in functions definition followed by its call
    ... return [bar, foo]; ... Evaluating the ArrayLiteral, the identifiers `foo` and `bar` must be resolved. ... The identifier `foo` is resolved on the VO, however `bar` cannot be resolved to an identifier and so a ReferenceError is produced. ...
    (comp.lang.javascript)