473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 2917
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Teis Draiby | last post by:
I want to write a base class that includes a member function that creates an instance of a derrived class and returns a pointer to it. Problem: The derived class definition has to follow the base...
13
by: Adam H. Peterson | last post by:
I just made an observation and I wondered if it's generally known (or if I'm missing something). My observation is that static protected members are essentially useless, only a hint to the user. ...
10
by: John Brock | last post by:
I have a base class with several derived classes (I'm writing in VB.NET). I want each derived class to have a unique class ID (a String), and I want the derived classes to inherit from the base...
3
by: Kirk Marple | last post by:
Just want to see if this is 'by design' or a bug... I have a common List<T> defined in a base class, and the base class has a static property to expose this list. I wanted the derived class to...
3
by: Elia Karagiannis | last post by:
The static constructor of a derived class never gets called if it has no other methods and the base class is only static I have the following base class and derived class. public class MyBase...
12
by: Hemanth | last post by:
Hi, I have a base class with a static constructor and some abstract methods. Derived classes implement these methods. From articles on the web, it appears that there is no guarentee that this...
5
by: none | last post by:
I'd like to create a new static property in a class "hiding" the property present in a base class. Since this needs to happen at runtime I tried doing this via DynamicMethod. But obviously the...
13
by: learning | last post by:
Hi I have a static class written by other team which encapsulates a database instance. but I need to extend it to incldue other things. I know that C# static class is sealed and can;t be inherited...
8
by: Mayur H Chauhan | last post by:
All, For my knowledge, if I declare Class as follow, then it thows compilation error. Protected Class Book End Class Even same for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.