?
Solved

Control class instances

Posted on 2003-11-22
2
Medium Priority
?
262 Views
Last Modified: 2010-04-02
I have a class A, and a class B.  I want to make it so I can only create a class A from class B.  This is how Im trying to do it now:

class A
{
protected:
  A() {};
  A(int i) {};
  ~A() {};
};

class B : public A
{
public:
   B() {};
  ~B() {};
  A* CreateA(int i)
  {
    A* a = new A(i);
    return a;
  }
};

This isnt working though, because it's saying class B cant access protected members of class A (although I thought a derived class could access protected members).  How can I accomplish what Im trying to do?
0
Comment
Question by:lwinkenb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 1000 total points
ID: 9804149
>>it's saying class B cant access protected members of class A

Try

class A
{
protected:
 A() {};
 A(int i) {};
 ~A() {};
 A* CreateA (int i) { return new A(i);}
};

class B : public A
{
public:
  B() {};
 ~B() {};
 A* CreateA(int i)
 {
   return A::CreateA(i);
 }
};
0
 
LVL 3

Expert Comment

by:ashoooo
ID: 9804383
B cant access the protected constructor of class A.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses
Course of the Month11 days, 14 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question