Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Control class instances

Posted on 2003-11-22
2
Medium Priority
?
268 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

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…
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 clear a vector as well as how to detect empty vectors in C++.

618 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