C++ reference sub class in super class - c++

I am little bit confused on referencing a sub class inside a super class in C++.
For example, given Java :
public class Entity {
protected ComplexEntity _ce;
public Entity() {
}
public ComplexEntity getCentity() {
return _ce;
}
}
Where ComplexEntity extends the entity.It works.In the sub class I call getCentity() no errors.
Now ,in C++ when I write something like that:
#pragma once
#include "maininclude.h"
#include "ExtendedEntity.h"
using namespace std;
class EntityBase
{
public:
EntityBase(void);
EntityBase(const string &name);
~EntityBase(void);
protected:
ExtendedEntity* _extc;
string _name;
};
I am getting compiler error:
error C2504: 'Entity' : base class undefined
In the classes which inherit from this Entity.Why does that happen?
Is it completely unacceptable in C++?
May be Entity must be abstract ?
I would like to get suggestions on possible workarounds.

You may consider using CRTP, cut/paste from Wikipedia:
// The Curiously Recurring Template Pattern (CRTP)
template<class Derived>
class Base
{
Derived* getDerived() { return static_cast<Derived*>(this); }
};
class Derived : public Base<Derived>
{
// ...
};

Your code is like the following:
struct D : B {}; // error: B doesn't mean anything at this point
struct B {
D *d;
};
Your header ExtendedEntity.h is trying to use the definition of Entity before Entity is defined.
You need to change your code to this:
struct D;
struct B {
D *d;
};
struct D : B {};

A class in C++ needs to know the size of all its members and of all its superclasses. Class Entity does not know the size of it's subclass ComplexEntity, unless class ComplexEntity is defined before class Entity. But then, class ComplexEntity does not know the size of its superclass Entity.
This problem exists in C++, because class members are accessed using simple offset calculation. You can work around this, by forward declaring the derived class and using pointers as members:
class Extended; // declare the derived class
class Base { // define the base class
Extended* e; // you cannot use Extended e here,
// because the class is not defined yet.
};
class Extended : public Base {}; // define the derived class

Related

Is it possible to cross cast through a templated class via dynamic_cast?

I'm dealing with an inheritance/casting problem and was wondering if anyone could help!
We used to have a class structure where class B inherits from class A as follows:
class A {
...
}
class B : public A {
...
}
class C : public B {
...
}
However, we're now implementing a new class structure where A and B inherit from base classes as follows:
template <typename T>
class ABase {
// all of the current A's logic will be moved to here
}
template <typename T>
class BBase : public ABase<T>{
// all of the current B's logic will be moved to here
}
class A: ABase<AClass> {
// nothing here
};
class B: BBase<BClass> {
// nothing here
}
class C : public B {
...
}
The structure is illustrated in the linked picture.
picture
I'm attempting to do a dynamic_cast from class C to class A but I'm getting a nullptr. It appears as if B cannot cast to BBase. Is it possible to cast from class B to class A? Is it possible to cast from class C to class A? Thanks!
nullptr example: jdoodle.com/ia/fBE

Member function that returns a doubly-nested class inside itself which inherited from a class inherited from itself

Say I have 3 classes with following topology, and the actual code is somehow like this:
// formal system.h
#pragma once
namespace nianyi {
class Proposition {
public:
class Term;
class Expression;
// Term::Value evaluate();
};
class Proposition::Term : public Proposition {
public:
class Variable;
class Value;
};
class Proposition::Term::Value : public Proposition::Term {
};
};
It's easy to see that if I uncomment that function declaration, the compiler is gonna complain about the un-previously-defined class Value, and indeed it does:
error C2027: use of undefined type 'nianyi::Proposition::Term'
But on the other hand, class Term is derived from its base class Proposition, which means I can't put its implementation in the base class since it's not a complete class before the implementation ends, and an incomplete class cannot be derived from.
Thus, putting the implementation of class Term both inside & outside class Proposition would lead to a compiler error. The inside one is caused by the incompleteness of the base class, and the outside one is caused by the incompleteness of the derivative.
So... what's the best way to solve this?
P.S. please stop telling "you should complete the definition before using a class" or "just don't make them nested".
For the first kinda words, that's the whole problem there, simply telling this doesn't really help much;
and for the second kinda words, I'm just intended to make them nested to not to expose those details.
This should work, not clear if its what you want though
class Proposition {
public:
};
class Term : public Proposition {
public:
class Variable {};
class Value {};
};
class Value : public Term {
};
Here is an abstract solution.
No need for inheritance.
class Variable{
};
class Value{
};
class Term{
class Variable;
class Value;
};
class Expression{
class Variable;
class Value;
};
class Proposition{
class Expression;
class Term;
};
It seems like no one adresses the point in your question title that talks about returning a “doubly nested class inside its self”.
I have seen this done a lot recently by making a private (or public if you like) member of the class and pointer instance of the class. This way you can generate and return a pointer to the class to this member.
// formal system.h
#pragma once
namespace nianyi {
class Proposition {
public:
class Term;
class Expression;
// Term::Value evaluate();
Proposition( )
{
Proposition *pinstance;
prop_ = pinstance;
}
private:
Proposition *prop_;
};
class Proposition::Term : public Proposition {
public:
class Variable;
class Value;
};
class Proposition::Term::Value : public Proposition::Term {
};
};

Need objects of inheriting classes to be visible before classes are defined

Usually you would declare and define the base class and then do the same for the inheriting class before declaring objects of the inheriting class. Unfortunately for for visibility reasons I need to declare the objects before the inheriting class is defines (although after the base class is defined).
I though that I could make a forward declaration for the inheriting class but if I do it like this:
class BaseClass
{
//content
};
class InheritingClass;
It does not recognise that it is inheriting but If i do it like this.
class BaseClass
{
//content
};
class InheritingClass: public BaseClass;
The compiler expects me to finish the definition. Is it possible to make the situation I am describing work?
I tried this in a compiler. You can declare another class identical to base, and use it for type checking, and then derive from that when you actually define it. Presumably this will be a subcategory of your base class that you are looking for. However, it won't be the exact type. If you're not looking to partition the hierarchy, then I still think the base class is what you should be using, since that is what you are looking for.
class base {int unused;};
class base_specific : public base {};
class actual : public base_specific {
public:
actual (int i) : m(i) {}
int m;
};
int main(){
actual x(4);
}
Forward declarations are not definitions. Therefore class InheritingClass: public BaseClass; doesn't make any sense. You can however make forward declarations and then later define it:
class base;
class derived;
// ...
class base {};
class derived : public base {};

c++ How do you call a templated base-class function from derived class instance

Found related questions but not the exact variant so I am posting a very simple question.
A derived class inherits from a templated base, and I want to call the base function, how to do it?
template <class A>
class testBase {
public:
void insert(const A& insertType) {
// whatever
}
};
class testDerived : testBase<double> {
// whatever
};
int main() {
testDerived B;
// Compiler doesn't recognize base class insert
// How do you do this?
B.insert(1.0);
}
Need public inheritance (default is private for class):
class testDerived : public testBase<double> {
A class has a default access level of 'private'. You basically inherited 'testBase' using private inheritance so that testBase's public interface is not part of testDerived's. Simple solution:
class testDerived: public testBase<double> {...};
I do wish C++ applied public inheritance by default though since that's generally a much more common case. Then again, we could just all use structs instead. :-D

Public member without inheritance

I have a base class which looks something like this:
class Base
{
public:
typedef std::shared_ptr<Base> ptr_t;
typedef std::weak_ptr<Base> wptr_t;
enum class Type { foo, bar, baz };
Type x;
// ...
};
I'd like those internal types to be public so that I can do stuff like Base::ptr_t my_ptr(new Base); and so on. But if I make a new class like this...
class Derived : public Base
{
// ...
};
unfortunately, Derived::ptr_t is still a Base pointer. I'd like Derived to publicly inherit x from Base, but not inherit ptr_t,wptr_t, or Type. For example
Derived a;
a.x = Base::Type::foo; // this should work
a.x = Derived::Type::foo; // but I want this to fail
Is this possible, perhaps though some magic use of friend or virtual or something like that?
Simply override the type:
class Derived {
typedef int Type;
};
It will not allow the use of Derived::Type (as it's private as well as typedefed)
class Derived : public Base
{
// This is now private
using Base::ptr_t;
using Base::wptr_t;
// ...
};
Based on the answers of iammilind and Luc Danton, here's what I've come up with:
class Base
{
private:
// only 'BaseClass' is allowed to derive from Base
Base() { }
friend class BaseClass;
public:
typedef std::shared_ptr<Base> ptr_t;
typedef std::weak_ptr<Base> wptr_t;
enum class Type { foo, bar, baz };
Type x;
// ...
};
class BaseClass : public Base
{
private:
// make all the raw_Base types private
using Base::ptr_t;
using Base::wptr_t;
using Base::Type;
};
class Derived : public BaseClass
{
// define as usual, and now all is well in the world.
};
// now, to test it
class Derived2 : public Base { }; // fails
Derived d;
d.x = Derived::Type::foo; // fails
d.x = Base::Type::foo; // works
// Which is exactly what I wanted.
As far as I can tell, the only problem with this solution is that it adds a new and potentially confusing class. The class is defined in such a way that it can't really be misused – Base itself cannot be derived from except by BaseClass, but still, BaseClass is an unattractive piece of namespace-clutter.
However, for the particular piece of code that I intend to use this in, I happen to be using the equivalent of BaseClass already to solve an unrelated problem. So this BaseClass technique suits my purposes just fine.