How can I deal with cyclic composition in C++? - c++

I'm trying to create a cyclic composition in C++ but I'm dealing with declaration problems. How could I solve them?
This is an example, class A contains a vector of B objects, but class B needs A to be declared first because it's needed in its constructor:
class A {
private:
std::vector<B> sons;
public:
void create_son() {
B obj(this);
sons.push_back(obj);
obj.some_method();
}
};
class B {
private:
A* parent;
public:
B (A* _parent) { parent = _parent; }
void some_method() {}
};

In class A, you use object of class B, so the complete definition of class B is needed. To solve this, put class B definition above class A. At the same time, in class B you work only with pointer to A, so you don't need the complete definition of class A: declaration is enough there.
So, add forward declaration of class A above class B definition.
class A;
class B {
private:
A* parent;
public:
B (A* _parent) { parent = _parent; }
void some_method() {}
};
class A {
private:
std::vector<B> sons;
public:
void create_son() {
B obj(this);
sons.push_back(obj);
obj.some_method();
}
};

You can forward declare pointer types:
class A; // sufficient to fully determine B
class B {
private:
A* parent;
public:
B (A* _parent) { parent = _parent; }
void some_method() {}
};
// now we have B defined, we can define A
class A {
private:
std::vector<B> sons;
public:
void create_son() {
B obj(this);
sons.push_back(obj);
obj.some_method();
}
};

Related

Cyclic dependency involving global objects C++

So, I'm in this situation right now. I have two classes A and B. The B is subclass of A and there's also a global object of the B class which is initialized in the main. A function of the A class uses that global object and calls its functions. In what order do I have to write the declarations in order for the compiler to read everything?
I keep getting the same errors whatever I try. Namely:
- (x) does not name a type
- invalid use of incomplete type (x)
- forward declaration of (x)
Code example:
class B;
B* B_GLOBAL;
class A{
public:
void A_function(){
B_GLOBAL->B_function();
}
private:
};
class B : public A{
public:
void B_function();
private:
};
int main(void){
B_GLOBAL = new B;
return 0;
}
Move the definition of A_function below the declaration of B:
class B;
B* B_GLOBAL;
class A{
public:
void A_function();
private:
};
class B : public A{
public:
void B_function();
private:
};
void A::A_function(){
B_GLOBAL->B_function();
}
int main(void){
B_GLOBAL = new B;
return 0;
}

Class with a std::vector of another class objects

I have two classes ( A and B)
I need to do something that when I make an object of class A (i.e. A obj()) a vector of class B that points to the objects of class A be constructed.
i.e.
if I make an object of class A named obj(), then I want the first element of vector in class B (i.e. vector<'A*'> objects ) to be declare by obj().
objects[0] = obj()
The code:
class B;
class A
{
public:
A(int _location)
{
location = _location;
pointer_B->redefine(this); // here in this line(14) I get two errors
}
private:
int location;
B* pointer_B;
};
class B
{
public:
void redefine(A* cur_obj)
{
objects.push_back(cur_obj);
}
private:
vector<A*> objects;
};
the errors are:
use of undefined type B (line 14)
left of '->redefine' must point to class/struct/union/generic type (line 14)
As #IgorTandetnik pointed out in the comments you have circular dependency between the class A and B. The solution is to separate the declarations and definitions either to header-source files and include the headers accordingly or put the definition of functions after the declaration of classes in the same translation unit.
class B;
class A
{
public:
A(int _location);
// .... other declarations
};
class B
{
public:
void redefine(A* cur_obj);
// ...
};
// definitions
A::A(int _location) {
location = _location;
pointer_B->redefine(this);
}
void B::redefine(A* cur_obj) {
objects.push_back(cur_obj);
}
Other remarks:
The member pointer_B is un-initialized in the constructor of
A. This is bad because it will lead to undefined
behaviour.
Hence initialise appropriately before dereferencing it.
Always prefer member initializer
lists
to initialize the class members: Read more in the following post: Why should I prefer to use member initialization list?
That means, change to:
class A
{
public:
explicit A(int _location, B *obj);
//^^^^^^^ ^^^^^^^^^
....
}
A::A(int _location, B *obj)
: location{ _location }
, pointer_B{ obj }
{
pointer_B->redefine(this);
}
```
class B;
class A
{
public:
A(int _location);
private:
int location;
B* pointer_B;
};
class B
{
public:
void redefine(A* cur_obj)
{
objects.push_back(cur_obj);
}
private:
vector<A*> objects;
};
A::A(int _location)
{
location = _location;
pointer_B->redefine(this);
}

unique pointer of base class with derived member

For my C++ program I have a lot of classes where a member should be of one of two types which have the same base class.
I thought I could implement this with pointers but I don't get it to work.
Example: lets assume we have a class A with a member b_ of class B:
class A{
public:
A(B b): b_{b} {}
private:
B b_;
}
The class B has only one function which is pure virtual:
class B{
public:
virtual void print() = 0;
}
now I have two derived classes of B and I want to change A in a way, that it could hold eihther objects of class B1 or B2:
class B1: public B{
public:
void print(){cout << "B1\n";}
}
class B2: public B{
public:
void print(){cout << "B2\n";}
}
My plan was to use unique pointers:
class A{
public:
A(std::unique_ptr<B> b): b_{std::move(b)} {}
private:
std::unique_ptr<B> b_;
}
int main(){
std::unique_ptr<B> b;
if (some condition){
b = make_unique<B1>(new B1()) ///?
}else{
b = make_unique<B2>(new B2()) ///?
}
A(b);
A.getB()->print();
}
There are several mistakes on your code. First of, you can't have two definitions of A. Second, you must pass the unique_ptr as r-value reference (std::move) since it is not copyable. Last, make a variable of type A (a) and then call methods on it.
#include <memory>
#include <iostream>
using namespace std;
class B{
public:
virtual void print() = 0;
virtual ~B() {};
};
class B1: public B{
public:
void print(){cout << "B1\n";}
};
class B2: public B{
public:
void print(){cout << "B2\n";}
};
class A{
public:
A(std::unique_ptr<B> b): b_{std::move(b)} {}
auto *getB() { return b_.get(); }
private:
std::unique_ptr<B> b_;
};
int main()
{
std::unique_ptr<B> b;
if(false)
b = make_unique<B1>();
else
b = make_unique<B2>();
A a(std::move(b));
a.getB()->print();
}

C++ member access from a derived class of a templated type

Long story short, what I want here is to declare a templated type in a base class and be able to access that type A<T> such that the base class B contains it and the derived class C is able to access it as C::A<T>. I did try declaring an int inside of class B and that can be accessed from the derived C class as C::int, here's the error!
||In constructor ‘D::D()’:|
|74|error: no match for ‘operator=’ (operand types are ‘A<C*>’ and ‘A<B*>’)|
|4|note: candidate: A<C*>& A<C*>::operator=(const A<C*>&)|
|4|note: no known conversion for argument 1 from ‘A<B*>’ to ‘const A<C*>&’|
And this is the code that does compile ( comment A<B*> i; and uncomment A<C*> i; to get the error).
#include <iostream>
//class with a template parameter
template <class a>
class A
{
private:
int somevalue;
public:
A(){}
~A(){}
void print()
{
std::cout<<somevalue<<std::endl;
}
};
//1. could forward declare
class C;
class B
{
protected:
A<B*> i;
//2. and then use
//A<C*> i;
public:
B(){}
~B(){}
A<B*> get()
{
return i;
}
/*
//3. use this return instead
A<C*> get()
{
return i;
}
*/
};
//specialization of B that uses B's methods variables
class C : public B
{
protected:
public:
C(){}
virtual ~C(){}
void method()
{
B::i.print();
}
};
//class D that inherits the specialization of C
class D : public C
{
private:
A<B*> i;//works
//4. but I want the inherited type to work like
//A<C*> i;// so that the type C* is interpreted as B*
public:
D()
{
this->i = C::i;
}
~D(){}
};
///////////////////////////////////////////////////////////////////////
int main()
{
D* d = new D();
delete d;
return 0;
}
But okay what if we tried this std::list<template parameter> LIST and then plug that in? That's the problem A<T> is std::list.
As far as I understand your issue now you seem to have a std::list<Base *> (renamed B to Base for clarity) and want to fill an std::list<Concrete*> (renamed C to Concrete, it's derived from Base) with it.
For that you need to iterate over the Base* pointers, checking for each whether it can be downcast to a Concrete* and if so adding it to the std::list<Concrete*>. You need to think about what to do if the downcast fails, too.
For all of this to work your Base needs to be a polymorphic base class, that is it must contain a virtual member function (don't forget to make the destructor virtual). Also note that this sounds like a catastrophe waiting to happen in terms of managing ownership of those pointers.
template<typename Base, typename Concrete>
std::list<Concrete*> downcast_list (std::list<Base*> const & bases) {
std::list<Concrete*> result;
for (auto const base_ptr : bases) {
Concrete * concrete_ptr = dynamic_cast<Concrete*>(base_ptr);
if (concrete_ptr != nullptr) {
result.push_back(concrete_ptr);
} else {
// Error or ignore?
}
}
return result;
}
Note: a more idiomatic version of this would use iterators.
I found the pattern to my problem, it's actually really simple and it serves as the base for encapsulating a class type a (which is a template parameter to be passed around, try looking at my question as a reference to class a). The pattern is shown below, it's generally what I wanted. I found it on this webpage Using Inheritance Between Templates chapter 7.5 from the book entitled OBJECT-ORIENTED
SOFTWARE DESIGN
and CONSTRUCTION
with C++ by Dennis Kafura. I'll copy it below the edited code for the sake of future reference in case anyone else needs it.
template <class a>
class B
{
private:
public:
B();
~B();
};
template <class a>
class C : public B<a>
{
public:
C();
~C();
};
This is the code it was adapted from.
template <class QueueItem> class Queue
{
private:
QueueItem buffer[100];
int head, tail, count;
public:
Queue();
void Insert(QueueItem item);
QueueItem Remove();
~Queue();
};
template <class QueueItem> class InspectableQueue : public Queue<QueueItem>
{
public:
InspectableQueue();
QueueItem Inspect(); // return without removing the first element
~InspectableQueue();
};
Try changing this:
#include <iostream>
//class with a template parameter
template <class a>
class A {
private:
int somevalue;
public:
A(){}
~A(){}
void print() {
std::cout<<somevalue<<std::endl;
}
};
//1. could forward declare
class C;
class B {
protected:
A<B*> i;
//2. and then use
//A<C*> i;
public:
B(){}
~B(){}
A<B*> get() {
return i;
}
/*/3. use this return instead
A<C*> get() {
return i;
} */
};
//specialization of B that uses B's methods variables
class C : public B {
protected:
public:
C(){}
virtual ~C(){}
void method() {
B::i.print();
}
};
//class D that inherits the specialization of C
class D : public C {
private:
A<B*> i;//works
//4. but I want the inherited type to work like
//A<C*> i;// so that the type C* is interpreted as B*
public:
D() {
this->i = C::i;
}
~D(){}
};
int main() {
D* d = new D();
delete d;
return 0;
}
To Something Like This:
#include <iostream>
//class with a template parameter
template <typename T>
class Foo {
private:
T value_;
public:
Foo(){} // Default
Foo( T value ) : value_(value) {}
~Foo(){}
void print() {
std::cout<< value_ << std::endl;
}
};
class Derived;
class Base {
protected:
Foo<Base*> foo_;
Base(){} // Default;
virtual ~Base(){}
// Overload This Function
template<typename T = Base>
/*virtual*/ Foo<T*> get();
/*virtual*/ Foo<Base*> get() { return this->foo_; }
/*virtual*/ Foo<Derived*> get();
};
class Derived : Base {
public:
Derived() {}
virtual ~Derived() {}
void func() {
Base::foo_.print();
}
void Foo<Derived*> get() override { return this->foo_; }
};
And this is as about as far as I could get trying to answering your question...
There are objects that you are not using in your code
There are methods that aren't being called.
It is kind of hard to understand the direction/indirection
of what you mean to do with the inheritance tree.
You are inheriting from a base class without a virtual destructor
And probably a few other things that I can not think of off the top of my head right now.
I'd be more than willing to try and help you out; but this is as far as I can go with what you currently are showing.
EDIT -- I made changes to the base & derived classes and removed the virtual keyword to the overloaded function template declarations - definitions belonging to those classes.

How to declare a friend that is a member function of another not yet defined class in C++?

How I declare B's constructor to be a friend of A? I tried:
class A
{
private:
A();
public:
friend B::B();
};
class B
{
public:
B();
};
replace B:: with class;
class A
{
private:
A();
public:
friend class B;
};
class B
{
public:
B();
};