Solved

Control class instances

Posted on 2003-11-22
2
239 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
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 250 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

777 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