I am writing a library in C ++. I have two classes in my library, A and B I want to hide the A() constructor from any code that references my library. I also want class B be able to call constructor A() .
I come from C # background and remember little of my C ++. In C #, I simply declare the A() constructor as internal . I read that the closest way to do this in C ++ is through a combination of friend declarations and forward-declarations. How can I do it? Below are my three files:
hijras:
#pragma once class A { private: A(); };
Bh
#pragma once class A; class B { public: A createA(); };
B.cpp:
#include "Ah" #include "Bh" AB::createA() { A result;
I tried adding this to Ah:
public: friend A createA();
Instead, I tried adding this to Ah with the appropriate declaration:
public: friend AB::createA();
Instead, I tried to add extern class B; in Ah and made B a class as follows:
public: friend class B;
I'm at a loss.
I think it might be easier if the function B::createA() returns a pointer to object A , and not object A , but this will not be done in my case. I emulate a private API, and an API call returns an A object, not a pointer.
source share