Solved

Control class instances

Posted on 2003-11-22
2
218 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

706 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now