469,579 Members | 1,157 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,579 developers. It's quick & easy.

Static Method in base class accessing protected constructor of derived clas

Here is some test code I've been attempting to compile (Visual Studio
2003)

test.h:

class Base
{
protected:
Base() {}
public:
template <class T>
static T* GetInstance() {return new T;} /*ERROR HERE :( */
};
class Derived
{
protected:
Derived() {}
};

class SomeCode
{
static void SomeCodeForYa();

};

test.cpp:
#include "test.h"

void SomeCode::SomeCodeForYa()
{
Derived* T = Base::GetInstance<Derived>();
}

I consistently get a compiler error: "error C2248:
'Derived::Derived' : cannot access protected member declared in class
'Derived'"

This *seems* like it should be allowed so long as the template
parameter is a derived from base. Am I missing something here? Is this
legal C++?

Oct 22 '07 #1
4 2655
On Oct 22, 5:47 pm, "softwared...@gmail.com" <softwared...@gmail.com>
wrote:
Here is some test code I've been attempting to compile (Visual Studio
2003)

test.h:

class Base
{
protected:
Base() {}
public:
template <class T>
static T* GetInstance() {return new T;} /*ERROR HERE :( */

};

class Derived
{
protected:
Derived() {}

};

class SomeCode
{
static void SomeCodeForYa();

};

test.cpp:
#include "test.h"

void SomeCode::SomeCodeForYa()
{
Derived* T = Base::GetInstance<Derived>();

}

I consistently get a compiler error: "error C2248:
'Derived::Derived' : cannot access protected member declared in class
'Derived'"

This *seems* like it should be allowed so long as the template
parameter is a derived from base. Am I missing something here? Is this
legal C++?
Note, I noticed I left off the : public Base. I get the same error
irregardless

New test.h with correction:
Test.h:
#include "stdafx.h"

class Base
{
protected:
Base() {}
public:
template <class T>
static T* GetInstance() { return new T;}
};
class Derived
{
protected:
Derived() {}
};
class SomeCode
{
static void SomeCodeForYa();

};

Oct 22 '07 #2
On 2007-10-22 23:47, so**********@gmail.com wrote:
Here is some test code I've been attempting to compile (Visual Studio
2003)

test.h:

class Base
{
protected:
Base() {}
public:
template <class T>
static T* GetInstance() {return new T;} /*ERROR HERE :( */
};
class Derived
{
protected:
Derived() {}
};

class SomeCode
{
static void SomeCodeForYa();

};

test.cpp:
#include "test.h"

void SomeCode::SomeCodeForYa()
{
Derived* T = Base::GetInstance<Derived>();
}

I consistently get a compiler error: "error C2248:
'Derived::Derived' : cannot access protected member declared in class
'Derived'"

This *seems* like it should be allowed so long as the template
parameter is a derived from base. Am I missing something here? Is this
legal C++?
Base does not have access to any of the (non public) methods in Derived.
Since Derived's constructor is protected you can not create an instance
of Derived in a static member function of Base.

The other way around would work, if GetInstance() was a member of
Derived and returned a pointer to Base.

--
Erik Wikström
Oct 22 '07 #3
On Oct 23, 12:55 am, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-10-22 23:47, softwared...@gmail.com wrote:
[...]
The other way around would work, if GetInstance() was a member of
Derived and returned a pointer to Base.
Not if the constructor it wanted to use was protected.
Protected in Base only grants access in Derived to the Base
objects which are, and are known to be, actually Derived. Thus,
for example, the constructor of Derived can call a protected
constructor of Base on itself (in the initialization list),
because the object is a Derived. But for example:

class Base
{
protected:
void f() ;
} ;

class Derived : public Base
{
void g( Base* pb, Derived* pd ) ;
} ;

void
Derived::g(
Base* pb,
Derived* pd )
{
f() ; // legal: is this->f(), and this is
// known to point to a Derived.
pb->f() ; // illegal !!!
pd->f() ; // legal.
}

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oct 23 '07 #4
On Oct 23, 12:55 am, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-10-22 23:47, softwared...@gmail.com wrote:
[...]
The other way around would work, if GetInstance() was a member of
Derived and returned a pointer to Base.
Not if the constructor it wanted to use was protected.
Protected in Base only grants access in Derived to the Base
objects which are, and are known to be, actually Derived. Thus,
for example, the constructor of Derived can call a protected
constructor of Base on itself (in the initialization list),
because the object is a Derived. But for example:

class Base
{
protected:
void f() ;
} ;

class Derived : public Base
{
void g( Base* pb, Derived* pd ) ;
} ;

void
Derived::g(
Base* pb,
Derived* pd )
{
f() ; // legal: is this->f(), and this is
// known to point to a Derived.
pb->f() ; // illegal !!!
pd->f() ; // legal.
}

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oct 23 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by Adam H. Peterson | last post: by
3 posts views Thread by Kirk Marple | last post: by
3 posts views Thread by Elia Karagiannis | last post: by
13 posts views Thread by learning | last post: by
8 posts views Thread by Mayur H Chauhan | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.