C++ Type of Enclosing Class in Static Member Function

C++ type of enclosing class in static member function

The problem is that C++ is lacking a self keyword.

I typically write:

struct Foo
{
typedef Foo self;

static void bar()
{
self* ptr = nullptr;
}
};

I realise you still have to make sure the typedef is correct, but at least this way you can have it at the top of the type definition where you'll notice it.

With hackery, though, you can make this entirely autonomous.

this in unevaluated context in static member functions

I don't see how it matters that this appears within an unevaluated context, you've referred to something that doesn't exist in a static member function, so how is the compiler supposed to deduce the type of this within this context?

As a corollary, the type of this in a non-static member function is dependent on the cv-qualifier of said member function, decltype(this) would yield T const* if the member function were const, and T * if it weren't. Thus, the type is dependent on the context of the expression. In your example, the context has no this pointer.

To alleviate the pain of having to name the class you could add an alias for it.

class VeryVeryLongClassName
{
using self = VeryVeryLongClassName;
};

Does a static member inside a nested class have static duration for the enclosing class?

Will A and B be able to have different values for thing?

No they won't have different values. All instances will see the same value for thing; the nesting of the class has no impact here.

static member variables are "associated with the class" (i.e. over non-static members that are associated with the instances of the class). From cppreference;

Static data members are not associated with any object. They exist even if no objects of the class have been defined. If the static member is declared thread_local (since C++11), there is one such object per thread. Otherwise, there is only one instance of the static data member in the entire program, with static storage duration.

Live sample.

Can enclosing class access nested class?

Can enclosing class access nested class?

Yes, if the enclosing class have an instance (an object) of the nested class.

A class is a class is a class... Nesting doesn't matter, you must always have an instance of the class to be able to call a (non-static) member function.

How to access nested class member function from enclosing class where all the members are public in c++?

nested_class1.print(); isn't how you'd call the print member of an instance of nested_class1, even if it were declared before there.

The definition of MainClass::func1 must be after the definition of nested_class1, which must be after the definition of MainClass::nested_class.

#include <iostream>

class MainClass
{
public:
class nested_class;
void func1();
};

class MainClass::nested_class
{
public:
virtual void print() = 0;
};

class nested_class1: virtual public MainClass::nested_class{
public:
void print();
};

class nested_class2 : virtual public MainClass::nested_class{
public:
void print();
};

void MainClass::func1() {
nested_class1 obj;
obj.print();
}

void nested_class1::print(){
std::cout<<"This is for 1\n";
}

void nested_class1::print(){
std::cout<<"This is for 2";
}

int main(){
MainClass instance;
instance.func1();
}

See it live

Accessing a non static member of parent from a nested class's function

C++ internal classes are not like Java nested classes. There is no object inside another. They are just classes which namespace is another class, like Java static inner classes.

You have no access to the member x since it belongs to A, which is a completely unrelated class that has nothing (Inheritance, composite, etc) to do with B.

Nested class member access on C++11

To close this question I'll take this as an answer:

"No, it's not treated as a member of the class, it's just scoped inside of it like anything else. You'll need an instance of Enclosing to access it's members."

  • this and several other comments addresses the problem in my code. Basically this is something that remains true for C++11.

Nested Class member function can't access function of enclosing class. Why?

foo() is a non-static member function of A and you are trying to call it without an instance.

The nested class B is a seperate class that only has some access privileges and doesn't have any special knowledge about existing instances of A.

If B needs access to an A you have to give it a reference to it, e.g.:

class A {
class B {
A& parent_;
public:
B(A& parent) : parent_(parent) {}
void foobar() { parent_.foo(); }
};
B b_;
public:
A() : b_(*this) {}
};


Related Topics



Leave a reply



Submit