Access private members of a class instantiated using interface - c++

I have a class derived from an interface and a friend class of the derived class. I want to access the members of derived class which is instantiated as interface. It looks like this:
Interface:
class AInterface
{
public:
virtual ~AInterface() = default;
virtual void set(int a) = 0;
};
Derived class A with friend class B:
class B;
class A : public AInterface
{
public:
~A() override {}
void set(int a) override
{
mem = a;
}
private:
friend class B;
int mem = 0;
};
class B:
class B
{
public:
B()
{
a = new A();
a->set(3);
}
int get_a()
{
// Access mem since it's a friend class
return a->mem;
}
private:
AInterface *a;
}
main:
int main()
{
B *b = new B();
std::cout << b->get_a() << std::endl;
return 0;
}
The program does not compile saying AInterface has no member named 'mem'.
Do I need getter functions in the interface and implement it in A to achieve this or is there any other way to do it?

Now work
#include <iostream>
using namespace std;
class AInterface
{
public:
virtual ~AInterface() = default;
int getMem() { return mem; }
virtual void set(int a) = 0;
protected:
int mem = 0;
};
class A : public AInterface
{
public:
~A() override {}
void set(int a) override
{
mem = a;
}
};
class B
{
public:
B()
{
a = new A{};
a->set(3);
}
int get_a()
{
// Access mem since it's a friend class
return a->getMem();
}
private:
AInterface *a;
};
int main()
{
B *b = new B();
std::cout << b->get_a() << std::endl;
return 0;
}
Method which is override by child and is pure, should be virtual.
If each class (child) Interface, variable int mem should be protected in interface.
Now works fine, like you want.
Add getter getMem()
Version with friend
#include <iostream>
using namespace std;
class AInterface
{
public:
virtual ~AInterface() = default;
virtual void set(int a) = 0;
protected:
friend class B;
int mem = 0;
};
class A : public AInterface
{
public:
~A() override {}
void set(int a) override
{
mem = a;
}
};
class B
{
public:
B()
{
a = new A{};
a->set(3);
}
int get_a()
{
// Access mem since it's a friend class
return a->mem;
}
private:
AInterface *a;
};
int main()
{
B *b = new B();
std::cout << b->get_a() << std::endl;
return 0;
}

In your class B.
class B
{
//...
int get_a()
{
return a->mem; // but a is a AInterface* !!!
}
private:
AInterface *a; // That's not an A*, but an AInterface*
};
You have 2 options.
use dynamic_cast<>
int B::get_a()
{
A* p = dynamic_cast<A*>(a);
if (p)
return p->mem;
// a is not an A*, it's another AInterface*-type object !!!
// What should you do?
throw something_or_other(); // throw?
return -1; // return an error code?
}
// or maybe add.. so you can check for errors before calling get_a()
A* B::get_A_ptr() const
{
return dynamic_cast<A*>(a);
}
dynamic_cast works fine, but can slow down your app if you need to make frequent reads of a->mem.
Store a in an A*, which is probably what you meant to do from the start...
class B
{
// ...
private:
A* a; // now a->A::mem is visible.
};
Since you explicitly call new A in B's constructor, I think option 2 is better for your case.

Related

How do I call an overridden method that was declared after the class calling it?

Say I have this code:
class A
{
public:
A(){};
void some_func(){
std::cout << 1;
};
};
class B
{
A* my_A;
public:
B(A* a) : my_A{a} {};
void use_A()
{
my_A->some_func();
}
};
class C : public A
{
public:
C(){};
void some_func()
{
std::cout << 2;
}
};
int main()
{
C new_c;
B new_b(&new_c);
new_b.use_A();
}
Now this compiles just fine, however use_A() is calling the original some_func(), not the override declared after. How can I do that? I expect the program to print 2, not 1.
You need to declare A::some_func() as virtual in order for C to override it, eg:
class A
{
public:
A(){};
virtual void some_func(){ // <--
std::cout << 1;
};
};
class B
{
A* my_A;
public:
B(A* a) : my_A{a} {};
void use_A()
{
my_A->some_func();
}
};
class C : public A
{
public:
C(){};
void some_func() override // <--
{
std::cout << 2;
}
};
int main()
{
C new_c;
B new_b(&new_c);
new_b.use_A(); // prints 2, not 1
}
Online Demo

how to access with single shared_ptr to vector of shared_ptr element

i need to call the overidden function inside B class through C class but it gives "Base" as a output rather than "Derived". when I call this code part classC.getTargetB().example(); i need to get "Derived" as a output. how can i reach my goal in the code.
class A {
public:
A() {};
virtual void example() { std::cout << "Base" << std::endl; }
};
class B :public A {
protected:
std::string name;
public:
B() {};
B(std::string n) { name = n; }
void example() override { std::cout << "Derived" << std::endl; }
};
class C {
protected:
std::vector<std::shared_ptr<A>> ptr;
std::shared_ptr<A> targetBobject;
public:
void setB(std::shared_ptr<A> target) {
targetBobject = target;
}
A getTargetB() {
return *targetBobject;
}
};
int main()
{
A example;
std::vector<std::shared_ptr<A>> parent;
C classC;
std::shared_ptr<B> ptr1 = std::make_shared<B>("B1");
std::shared_ptr<B> ptr2 = std::make_shared<B>("B2");
parent.push_back(std::move(ptr1));
parent.push_back(std::move(ptr2));
//parent.at(0)->example(); this gives 'Derived' as a output
classC.setB(parent.at(0));
classC.getTargetB().example();// gives 'Base' as a output
}

Creating an initialization list of abstract base type references

I have two classes A,B which inherit from an abstract base class Abs. I would like to create an initialization list containing both of them. I would like to iterate on that list with a foreach loop.
class Abs {
public:
virtual ~Abs() = default;
virtual void f() = 0;
};
class A : public Abs {
virtual void f() override;
};
class B : public Abs {
virtual void f();
};
int main() {
A a;
B b;
const Abs& a_abs = a;
const Abs& b_abs = b;
for (const auto& abs : {a_abs, b_abs})
{
}
return 0;
}
The compilation of the for loop fails with:
error: cannot allocate an object of abstract type ‘Abs’
Why is it trying to allocate an Abs type?
How can I overcome that?
Thank you :)
there were a few things wrong with your code:
you should have a public destructor/constructor
B doesn't inherit from Abs therefore in can't be converted to Abs
i don't know if you can get what you want with references but with pointers you can. please use smart pointer this is just a prof of concept
#include <initializer_list>
#include <iostream>
class Abs {
public:
virtual ~Abs() = default;
virtual void f() const = 0;
};
class A : public Abs {
public:
virtual void f() const {
std::cout << "A" << std::endl;
}
};
class B : public Abs {
public:
virtual void f() const {
std::cout << "B" << std::endl;
}
};
int main() {
A* a = new A();
B* b = new B();
const Abs* a_abs = dynamic_cast<const Abs *>(a);
const Abs* b_abs = dynamic_cast<const Abs *>(b);
for (const auto* abs : {a_abs, b_abs})
{
abs->f();
}
delete a;
delete b;
return 0;
}

C++ elegantly clone derived class by calling base class

I have a need to clone a derived class given only a reference or pointer to the base class. The following code does the job, but doesn't seem elegant, because I'm putting boilerplate code into many derived classes C, D, E that are siblings of B (not shown) that just calls the default copy constructor of each. Isn't that what the default copy constructor is for, if only it could be virtual?
Is there a better way?
Making a virtual assignment operator would be wrong, as I don't want C to assign to B, B to D, etc, just clone B, C, D or E.
#include <iostream>
using namespace std;
class A {
public:
virtual ~A() {}
virtual A* clone()=0;
};
class B : public A {
int i;
public:
virtual A* clone() {
cout << "cloned B" << endl;
return new B(*this);
}
virtual ~B() { cout << "destroyed b" << endl; }
};
int main() {
A* a = new B();
A* aa = a->clone();
delete a;
delete aa;
return 0;
}
You could always stick all the cloning logic into its own class in the middle of the hierarchy:
template <class Derived, class Base>
class CloneCRTP : public Base {
public:
Derived* clone() const override {
return new Derived(static_cast<Derived const&>(*this));
}
};
And then:
class B : public CloneCRTP<B, A>
{
int i;
public:
virtual ~B() { cout << "destroyed b" << endl; }
};
No more boilerplate.
You can rely on the CRTP idiom.
It follows a minimal, working example:
struct B {
~virtual ~B() { }
virtual B* clone() = 0;
};
template<class C>
struct D: public B {
B* clone() {
return new C{*static_cast<C*>(this)};
}
};
struct S: public D<S> { };
int main() {
B *b1 = new S;
B *b2 = b1->clone();
delete b1;
delete b2;
}
To achieve clone that works as with covariant return type, there is a need for some more complicated CRTP:
class Shape {
// virtual
virtual Shape* do_clone() const = 0;
public:
virtual ~Shape() {}
// non-virtual
Shape* clone() const {
return do_clone();
}
// ...
};
template <class Derived, class Base>
class CloneCRTP : public Base {
// virtual
Base* do_clone() const override {
return new Derived(static_cast<Derived const&>(*this));
}
public:
// non-virtual
Derived* clone() const {
return static_cast<Derived*>(do_clone());
}
};
Usage:
class Rectangle: public CloneCRTP<Rectangle, Shape> { /*...*/ };
class Triangle: public CloneCRTP<Triangle, Shape> { /*...*/ };
int main() {
Rectangle* r1 = new Rectangle{};
// getting the proper type from clone:
Rectangle* rcopy = r1->clone();
delete rcopy;
delete r1;
Triangle t1{};
// getting the proper type from clone:
Triangle* tcopy = t1.clone();
delete tcopy;
}
Code: http://coliru.stacked-crooked.com/a/339458f8b45299c3
I do it in a generalized way.
Definition:
template <class Class>
class CloneInterface {
public:
Class* copy() const { return new Class{static_cast<Class const&>(*this)}; }
virtual Class* clone() const { return this->copy(); }
};
template <class Derived, class Base>
class CloneCRTP : public Base {
public:
Derived* copy() const { return new Derived{static_cast<Derived const&>(*this)}; }
Base* clone() const override { return this->copy(); }
};
Usage:
class Base : public CloneInterface<Base> {
private:
int a = 0;
public:
virtual ~Base() {}
void print() { std::cout << "Base: a = " << a << '\n'; }
};
class A : public CloneCRTP<A, Base> {
private:
int b = 1;
public:
void print() { std::cout << "A: b = " << b << '\n'; }
};
Example:
int main() {
auto b = new Base();
auto b_clone = b->clone();
auto b_copy = b->copy();
b->print(); // Base print()
b_clone->print(); // Base print()
b_copy->print(); // Base print()
std::cout << "=========================\n";
auto a = new A();
auto a_clone = a->clone(); // returns Base*
auto a_copy = a->copy(); // returns A*
a->print(); // A print()
a_clone->print(); // Base print(), because clone returns pointer to the base class
a_copy->print(); // A print()
std::cout << "=========================\n";
auto c = static_cast<Base*>(a);
auto c_clone = c->clone(); // returns Base*, but A* inside
auto c_copy = c->copy(); // returns Base*, that is really Base
c->print(); // Base print()
c_clone->print(); // Base print()
c_copy->print(); // Base print()
std::cout << "=========================\n";
auto a_clone_restored = static_cast<A*>(c_clone);
auto a_copy_restored = static_cast<A*>(c_copy); // Invalid
a_clone_restored->print(); // A print()
a_copy_restored->print(); // A print()
delete b;
delete b_clone;
delete b_copy;
delete a;
delete a_clone;
delete a_copy;
return 0;
}
This approach allows to reuse crtp definition in all hierarchies that require clone functionality.
Code: https://coliru.stacked-crooked.com/a/269a2427fd2f919b

Factory pattern using unique_ptr in c++

I have an old factory implementation in c++, and I want to use unique pointers instead of raw pointers in it. A minimal example of my code is as follows. I have a base class A, and a derived class B. In main(), I pass 1 to the create method in A, and the type of b1 is now changed to B.
#include <iostream>
#include <map>
class A {
public:
A() {}
virtual void Foo() {}
std::map<int, A *> &registerType() {
static std::map<int, A *> map_instance;
return map_instance;
}
A *create(int n) { return registerType()[n]; }
};
class B : A {
public:
B() { registerType()[1] = this; }
void Foo() { std::cout << "I am B!\n"; }
};
static B b0;
int main() {
A *b1 = new A();
b1 = b1->create(1);
b1->Foo();
return 0;
}
Now if I want to change raw pointers to unique pointers, I naturally get a collection of errors (the following code results in errors):
#include <iostream>
#include <map>
#include <memory>
class A {
public:
A() {}
virtual void Foo() {}
std::map<int, std::unique_ptr<A>> &registerType() {
static std::map<int, std::unique_ptr<A>> map_instance;
return map_instance;
}
std::unique_ptr<A> create(int n) { return registerType()[n]; }
};
class B : A {
public:
B() { registerType()[1](this); }
void Foo() { std::cout << "I am B too!\n"; }
};
static B b0;
int main() {
std::unique_ptr<A> b1(new A());
b1 = b1->create(1);
b1->Foo();
return 0;
}
The errors are:
In member function 'std::unique_ptr<A> A::create(int)':
use of deleted function 'std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = A; _Dp = std::default_delete<A>]'
std::unique_ptr<A> create(int n) { return registerType()[n]; }
In constructor 'B::B()':
no match for call to '(std::map<int, std::unique_ptr<A> >::mapped_type {aka std::unique_ptr<A>}) (B* const)'
B() { registerType()[1](this); }
^
So I want to know:
Were unique pointers intended to be used in cases like mine? (I assume the response should be yes!)
I need to pass this as a unique_ptr type to the registerType method. How I can pass the ownership of the pointer to the current instance (this keyword) to a unique_ptr? (If it is possible or was intended to be possible.)
If it is a good practice to use unique pointers here, how I should implement it?
First of all, if someone wants to implement a factory pattern, an acceptable way of doing it with raw pointers is as follows:
#include <iostream>
#include <map>
class A;
class A_Factory {
public:
A_Factory() {}
virtual A *create() = 0;
};
class A {
public:
A() {}
static void registerType(int n, A_Factory *factory) {
get_factory_instance()[n] = factory;
}
static A *create(int n) {
A *A_instance = get_factory_instance()[n]->create();
return A_instance;
}
virtual void setMyID(int n) {}
virtual void I_am() { std::cout << "I am A\n"; }
virtual ~A() {}
protected:
int MyID;
static std::map<int, A_Factory *> &get_factory_instance() {
static std::map<int, A_Factory *> map_instance;
return map_instance;
}
};
class B : public A {
public:
B() {}
void Foo() {}
void I_am() { std::cout << "I am B " << MyID << "\n"; }
void setMyID(int n) { MyID = n; }
~B() {}
private:
};
class B_Factory : public A_Factory {
public:
B_Factory() { A::registerType(1, this); }
A *create() { return new B(); }
};
static B_Factory b0_factory;
void caller() {}
int main() {
A *b1 = A::create(1);
A *b2 = A::create(1);
b1->setMyID(10);
b2->setMyID(20);
b1->I_am();
b2->I_am();
delete b1;
delete b2;
return 0;
}
A is the base class, and B is the derived one. If we pass 1 to A::create(int n), an object of type B will be produced. The memory is managed manually and there would be no memory leak.
Concerning the questions in the post:
YES. unique_ptr is awesome; use them wherever you can!
With the design presented in the question, passing the ownership of this was somehow necessary. I cannot think of a way to pass the ownership of this. With the design presented in the answer, it is not necessary to pass the ownership of this.
Implement the unique_ptr in the above factory pattern as below:
#include <iostream>
#include <map>
#include <memory>
class A;
class A_Factory {
public:
A_Factory() {}
virtual std::unique_ptr<A> create_unique() = 0;
};
class A {
public:
A() {}
static void registerType(int n, A_Factory *factory) {
get_factory_instance()[n] = factory;
}
static std::unique_ptr<A> create_unique(int n) {
std::unique_ptr<A> A_instance =
std::move(get_factory_instance()[n]->create_unique());
return A_instance;
}
virtual void setMyID(int n) {}
virtual void I_am() { std::cout << "I am A\n"; }
virtual ~A() {}
protected:
int MyID;
static std::map<int, A_Factory *> &get_factory_instance() {
static std::map<int, A_Factory *> map_instance;
return map_instance;
}
};
class B : public A {
public:
B() {}
void Foo() {}
void I_am() { std::cout << "I am B " << MyID << "\n"; }
void setMyID(int n) { MyID = n; }
~B() {}
private:
};
class B_Factory : public A_Factory {
public:
B_Factory() { A::registerType(1, this); }
std::unique_ptr<A> create_unique() {
std::unique_ptr<A> ptr_to_B(new B);
return ptr_to_B;
}
};
static B_Factory b0_factory;
void caller() {}
int main() {
std::unique_ptr<A> b1 = std::move(A::create_unique(1));
std::unique_ptr<A> b2 = std::move(A::create_unique(1));
b1->setMyID(10);
b2->setMyID(20);
b1->I_am();
b2->I_am();
return 0;
}
As you can see, no manual memory management is necessary and the memory management is handled by the unique_ptr.